| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
高端软件开发 = 年薪十万不是梦赛孚耐:软件保护加密专家身份认证令牌USB KEY 
共有 468 人关注过本帖
标题:石子合并问题(求助 改错)
收藏  订阅  推荐  打印 
diaoxue
Rank: 2
等级:注册会员
帖子:142
积分:1630
注册:2007-6-1
石子合并问题(求助 改错)

一列石子,公式得到了,用动态规划实现时总是输出0
我实现矩阵连乘算法用动态规划也不能得到正确结果
[code=c]
/*m[i][j]={0 i=j;min{m[i][k-1]+m[k][j]+w(i,j)} i<j}*/
#include <iostream>
using namespace std;
const int N=4;
int a[N]={4,4,5,9};
int m[N][N]={0};
int s[N][N]={0};
int Sum(int a[],int i,int j)//最后一次合并的得分
{
    int temp=0;
    for(int k=i;k<=j;k++)
    {
        temp+=a[k];
    }   
    return temp;
}

void Stone()//动态规划求最小得分
{
    int i,j,r,k,t;
    for(i=0;i<N;i++)
        m[i][i]=0;
    for(r=1;r<N;r++)
    {
        for(i=0;i<N-r;i++)
        {
            j=i+r;
            for(k=i+1;k<j;k++)
            {
                t=m[i][k-1]+m[k][j]+Sum(a,i,j);
                if(t<m[i][j])
                {
                    m[i][j]=t;
                    s[i][j]=k;
                }   
            }   
        }   
    }     
}
        
int main()
{
    Stone();  
    cout<<m[0][N-1]<<endl;  
    return 0;
}    [/code]
搜索更多相关主题的帖子: 石子  改错  
2007-12-22 20:58
HJin
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:27
帖子:401
积分:4134
注册:2007-6-9

i did NOT read your code line by line. I saw you have a formula at the beginning of the code and I guess you are implementing the formula.

My experience with DP is that you may need some boundary conditions, say

m[i][0] = 0 for all i // i.e., first column is 0
m[0][j] = 0 for all j // 1st row is 0

besides m[i][i] = 0

I may be wrong. But this is my point of view.

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-12-23 04:11
aipb2007
Rank: 12Rank: 12Rank: 12
来自:CQU
等级:贵宾
威望:40
帖子:2881
积分:29414
注册:2007-3-18

把题目连接给出来,就有人给你做做看了!

Fight  to win  or  die...
2007-12-23 10:42
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.061512 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved