Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1368 Accepted Submission(s):
574
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int N,M,e[105][105],dp[105][105][2];
int solve()
{
int i,j,k;
memset(dp,-inf,sizeof(dp)); dp[0][1][0]=0;
for(i=1;i<=N;++i) dp[i][1][0]=dp[i-1][1][0]+e[i][1];
for(i=2;i<=M;++i)
{
for(j=N;j>=1;j--) dp[j][i][1]=max(dp[j+1][i][1],max(dp[j][i-1][0],dp[j][i-1][1]))+e[j][i];
for(j=1;j<=N;j++) dp[j][i][0]=max(dp[j-1][i][0],max(dp[j][i-1][0],dp[j][i-1][1]))+e[j][i];
}
return max(dp[1][M][0],dp[1][M][1]);
}
int main()
{
int t,i,j,k;
cin>>t;
for(int tes=1;tes<=t;++tes){
cin>>N>>M;
for(i=1;i<=N;++i)
for(j=1;j<=M;++j) scanf("%d",&e[i][j]);
printf("Case #%d:\n%d\n",tes,solve());
}
return 0;
}
原文:http://www.cnblogs.com/zzqc/p/6965254.html