首页 > 代码库 > hdu 2666 Matrix
hdu 2666 Matrix
Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1332 Accepted Submission(s): 740
Problem Description
Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.
Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end.
Every time yifenfei should to do is that choose a detour which frome the top left point to the bottom right point and than back to the top left point with the maximal values of sum integers that area of Matrix yifenfei choose. But from the top to the bottom can only choose right and down, from the bottom to the top can only choose left and up. And yifenfei can not pass the same area of the Matrix except the start and end.
Input
The input contains multiple test cases.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
Output
For each test case output the maximal values yifenfei can get.
Sample Input
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
Sample Output
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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。