NYOJ 61传纸条(一) 双线程DP问题

时间:2025-01-08 08:04:07

http://www.cnblogs.com/HpuAcmer/archive/2012/05/06/2486591.html

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=61

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int f[][][];
int g[][];
int max(int x,int y)
{
return x>y?x:y;
}
int main()
{
int t,ans;
scanf("%d",&t);
while(t--)
{
int n,m,i,j,k,sum,t1,t2;
scanf("%d %d",&n,&m);
sum=m+n;
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
scanf("%d",&g[i][j]);
}
}
for(k=;k<=sum;k++)
{
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
if(i!=j&&k>=i&&k>=j)
{
t1=max(f[k-][i][j],f[k-][i][j-]);
t2=max(f[k-][i-][j-],f[k-][i-][j]);
f[k][i][j]=max(t1,t2)+g[i][k-i]+g[j][k-j];
}
}
}
}
ans=max(f[sum-][n-][n],f[sum-][n][n-]);
printf("%d\n",ans); }
//system("pause");
return ;
}