| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
dp思想 为什么wrong了除了第一排
下面每1个花是前一排的dp的最大值。。到最后再求最后一排的最大值为什么错了
dp[i][j]=dp[i-1][j-1]+map[i][j];
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define M 108
int map[M][M];
int dp[M][M];
int max(int x,int y)
{
if(x>y)
return x;
else
return y;
}
int main()
{
int i,j,k,r,c,t1;
while(~scanf("%d%d",&r,&c))
{
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
scanf("%d",&map[i][j]);
}
for(i=1;i<=r;i++)
for(j=1,t1=-100000;j<=c;j++)
{
dp[i][j]=-1000000;
}
for(i=1;i<=r;i++)
for(j=i,t1=-100000;j<=c;j++)
{
if(i==1)
{
dp[i][j]=max(t1,map[i][j]);
t1=dp[i][j];
}
else
{
dp[i][j]=(map[i][j]+dp[i-1][j-1]);
}
}
/* for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%9d ",dp[i][j]);
}
printf("\n");
}*/
for(i=1,t1=-10000000;i<=c;i++)
{
if(dp[r][i]>t1)
{
t1=dp[r][i];
}
}
printf("%d\n",t1);
// printf("%d\n",dp[r][c]);
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator