Matrix
Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 268664-bit integer IO format: %I64d Java class name: Main
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
Source
解题:传说中的双进程dp
1 #include <bits/stdc++.h> 2 using namespace std; 3 int dp[80][40][40],val[40][40],n; 4 int main() { 5 while(~scanf("%d",&n)) { 6 memset(dp,0,sizeof dp); 7 for(int i = 0; i < n; ++i) 8 for(int j = 0; j < n; ++j) 9 scanf("%d",val[i] + j); 10 for(int k = 1; k < 2*n - 2; ++k) { 11 for(int i = 0; i < n; ++i) 12 for(int j = 0; j < n; ++j) { 13 if(i == j) continue; 14 dp[k][i][j] = max(max(dp[k-1][i][j],dp[k-1][i-1][j]),max(dp[k-1][i][j-1],dp[k-1][i-1][j-1])); 15 dp[k][i][j] += val[i][k - i] + val[j][k - j]; 16 } 17 } 18 int t = 2*n-3; 19 printf("%d ",max(dp[t][n-1][n-2],dp[t][n-2][n-1]) + val[0][0] + val[n-1][n-1]); 20 } 21 return 0; 22 }