Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 848 Accepted Submission(s): 484
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.
Each case first line given the integer n (2<n<30)
Than n lines,each line include n positive integers.(<100)
让两个进程同时进行,枚举步数K,当x1==x2||y1==y2时跳过,得状态转移方程:
dp(k, x1, y1, x2, y2) = max(dp(k-1, x1-1, y1, x2-1, y2), dp(k-1, x1-1, y1, x2, y2-1), dp(k-1, x1, y1-1, x2-1, y2), dp(k-1, x1, y1-1,x2, y2-1))
+ data(x1, y1) + data(x2, y2) ;
由于只能走右或下,所以坐标满足x+y=k。这样就能降低维数为3维,方程:
dp(k, x1, x2) = max(dp(k-1, x1, x2), dp(k-1, x1-1, x2), dp(k-1, x1, x2-1), dp(k-1, x1-1, x2-1)) + data(x1, k-x1) + data(x2, k-x2) ;
#include<stdio.h>
#include<string.h>
#define MAX(a,b) a>b?a:b
int data[31][31], dp[62][31][31] ;
int maxx(int a,int b,int c,int d)
{
a = MAX(a,b) ;
c = MAX(c,d) ;
a = MAX(a,c) ;
return a ;
}
int main()
{
int n, i, j, k ;
while(scanf("%d",&n)!=-1)
{
for(i=0; i<n; i++)
for(j=0; j<n; j++)
scanf("%d", &data[i][j]) ;
memset(dp, 0, sizeof(dp)) ;
for(k=1; k<2*n-2; k++)
{
for(i=0; i<=k; i++)
{
for(j=0; j<=k; j++)
{
if(i==j||i>=n||j>=n)
continue ;
dp[k][i][j] = maxx(dp[k-1][i][j], dp[k-1][i-1][j], dp[k-1][i][j-1], dp [k-1][i-1][j-1]) ;
dp[k][i][j] += data[i][k-i] + data[j][k-j] ;
}
}
}
dp[k][n-1][n-1] = MAX(dp[k-1][n-2][n-1], dp[k-1][n-1][n-2]) + data[n-1][n-1] + data[0][0] ;
printf("%d\n", dp[k][n-1][n-1]) ;
}
return 0 ;
}