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

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

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

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 int f[105][54][54];
 5 int g[54][54];
 6 int max(int x,int y)
 7 {
 8     return x>y?x:y;
 9 }
10 int main()
11 {
12     int t,ans;
13     scanf("%d",&t);
14     while(t--)
15     {
16         int n,m,i,j,k,sum,t1,t2;
17         scanf("%d %d",&n,&m);
18         sum=m+n;
19         for(i=1;i<=n;i++)
20         {
21             for(j=1;j<=m;j++)
22             {
23                 scanf("%d",&g[i][j]);
24             }
25         }
26         for(k=2;k<=sum;k++)
27         {
28             for(i=1;i<=n;i++)
29             {
30                 for(j=1;j<=n;j++)
31                 {
32                     if(i!=j&&k>=i&&k>=j)
33                     {
34                         t1=max(f[k-1][i][j],f[k-1][i][j-1]);
35                         t2=max(f[k-1][i-1][j-1],f[k-1][i-1][j]);
36                         f[k][i][j]=max(t1,t2)+g[i][k-i]+g[j][k-j];
37                     }
38                 }
39             }
40         }
41         ans=max(f[sum-1][n-1][n],f[sum-1][n][n-1]);
42         printf("%d\n",ans);
43 
44     }
45     //system("pause");
46     return 0;
47 }
View Code

 

posted @ 2014-04-03 23:24  陈泽泽  阅读(223)  评论(0编辑  收藏  举报