标签:des style blog http java color
2 10 3 5 10 3 10 3 3 2 5 3 6 7 10 5 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
28 46 80
双线程DP
#include"stdio.h" #include"string.h" #define N 32 int num[N][N]; int f[N][N][N][N]; int Max(int a,int b) { return a>b?a:b; } int main() { int i,j,k,l,n; while(scanf("%d",&n)!=-1) { for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&num[i][j]); memset(f,0,sizeof(f)); for(i=1;i<=n;i++) //一条路线从(1,1)点向右出发 { for(j=1;j<=n;j++) { for(k=i+1;k<=n;k++) //二路线从第二行开始,即从(1,1)点向下出发 { l=i+j-k; //因为两条路线的步数相等,则二路线的纵坐标容易求得 if(l<=0) break; //该点的值可以从四个路线得到,下下,右右,下右,右下 f[i][j][k][l]=Max(Max(f[i-1][j][k-1][l],f[i][j-1][k][l-1]),Max(f[i-1][j][k][l-1],f[i][j-1][k-1][l])); f[i][j][k][l]+=(num[i][j]+num[k][l]); //加上该路线的值 } } } int t=Max(Max(f[n-1][n][n-1][n],f[n][n-1][n][n-1]),Max(f[n-1][n][n][n-1],f[n][n-1][n-1][n])); printf("%d\n",t+num[n][n]+num[1][1]); //起点和终点的值 } return 0; }
hdu 2666 Matrix,码迷,mamicode.com
标签:des style blog http java color
原文地址:http://blog.csdn.net/u011721440/article/details/24719561