2014
02-13

# Matrix

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.

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)

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)

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

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAXN = 33;
const int INF = 9999999;

int dp[MAXN][MAXN][MAXN][MAXN];
int maze[MAXN][MAXN];
int N;
int dir[4][4] = {1,0,1,0,1,0,0,1,0,1,1,0,0,1,0,1};

int dfs(int x1,int y1,int x2,int y2)
{
if (maze[x1][y1] == -1 || maze[x2][y2] == -1)return 0;
if (x1 + y1 == N + N)return maze[N][N];
if (dp[x1][y1][x2][y2] != -1)return dp[x1][y1][x2][y2];
if (x1 == x2 && y1 == y2 && x1 + y1 > 2)return 0;
int ans = 0;
for (int i = 0;i < 4;i++)
{
int a = x1 + dir[i][0];
int b = y1 + dir[i][1];
int c = x2 + dir[i][2];
int d = y2 + dir[i][3];
int temp = dfs(a,b,c,d);
ans = max(ans,temp);
}
return dp[x1][y1][x2][y2] = ans + maze[x1][y1] + maze[x2][y2];
}

int solve()
{
memset(dp,-1,sizeof(dp));
return dfs(1,1,1,1) - maze[1][1];
}

int main()
{
while (scanf("%d",&N) != EOF)
{
memset(maze,-1,sizeof(maze));
for (int i = 1;i <= N;i++)
for (int j = 1;j <= N;j++)
scanf("%d",&maze[i][j]);
printf("%d\n",solve());
}
return 0;
}