码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2666 Matrix

时间:2014-07-22 23:03:13      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   

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. 
 

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)
 

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;
}



hdu 2666 Matrix,码迷,mamicode.com

hdu 2666 Matrix

标签:des   style   blog   http   java   color   

原文地址:http://blog.csdn.net/u011721440/article/details/24719561

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!