2853 Square Game (3D Board), 2853 square board
Time Limit: 1 s space limit: 128000 KB title level: DiamondQuestionView running resultsDescriptionDescription
The dish saw a game called the Square Game ~
The game rules are as follows:
In an n * n grid, you can get a certain number of points in each 1*1 grid. Note that the upper left corner is () and the lower right corner is (n, n ). Players need to select a path from () to (n, n) and obtain the reward points on the path. There are also requirements for the path. The requirement is that the path can only go from (x, y) to (x + 1, y) or (x, y + 1) ]. It reaches (n, n) in 2n-1 regions ). Of course, you can win the highest point.
At this time, his friend Yue was seen, so she was invited to come to the Japanese version. The rule of the simplified version is that the route for adding a single version to a single version cannot be the same. Yue knows that his food is very clever and he is unwilling to play with him if he is afraid of losing too much. The dish can be flustered, so a fair value D is defined. The fair value is equal to the sum of the absolute values of the subtract deviation for the points obtained by the two paths, that is, D = sigma (| kxi-kyi |) | (kx and ky are dishes, respectively, and the jungle coefficient of each region that the month and month walk through ). However, this is a huge computing task. I found you for the dish. Please help calculate the maximum value of fair value.
Input description
Input Description
The first line is a positive integer n.
The next n rows, n integers in each line, represent the fairness of each region in the jungle
Output description
Output Description
An integer Dmax, that is, the maximum value of the fair value.
Sample Input
Sample Input
4
1 2 3 4
1 5 3 2
8 1 3 4
3 2 1 5
Sample output
Sample Output
13
Data range and prompt
Data Size & Hint
For 20% of the data, ensure that 0 <n ≤ 20
For 50% of the data, ensure that 0 <n ≤ 50
For 100% of data, ensure 0 <n ≤ 100 and for all I, j guarantee | Kij | ≤ 300
First, the four dimensions of this question will lead to space explosion.
So I learned how to use 3D representation.
Prerequisites:
1. Set the coordinates of two people to A and B respectively;
2. the number of steps required for the square n * n from (1, 1) to (n, n) is 2 * n + 1 (n horizontal columns and n columns, one of them is repeated)
We use k to represent the number of steps.
We use I to represent that the abscissa of step A in step k is
In j, the abscissa of step k B is j.
Then the coordinates of A can be expressed as (I, k-I + 1)
The coordinates of B can be expressed as (j, k-j + 1)
For each vertex (I, j)
There are also four transfer cases
1. A from top to bottom dp [I-1] [j] [k-1]
2. B from top to bottom dp [I] [J-1] [k-1]
3. AB simultaneously from top to bottom dp [I-1] [J-1] [k-1]
4. AB simultaneously from left to right dp [I] [j] [k-1]
Then add the value of this vertex.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<cstdlib> 6 using namespace std; 7 void read(int &n) 8 { 9 char c='+';int x=0;bool flag=0;10 while(c<'0'||c>'9')11 {c=getchar();if(c=='-')flag=1;}12 while(c>='0'&&c<='9')13 {x=x*10+(c-48);c=getchar();}14 flag==1?n=-x:n=x;15 }16 int n;17 int dp[101][101][301];18 int a[101][101];19 int ans=0;20 int main()21 {22 read(n);23 for(int i=1;i<=n;i++)24 for(int j=1;j<=n;j++)25 read(a[i][j]);26 // a :(i,k-i+1)27 // b :(j,k-j+1)28 for(int k=1;k<=2*n-1;k++)29 {30 for(int i=1;i<=n&&i<=k;i++)31 for(int j=1;j<=n&&j<=k;j++)32 {33 dp[i][j][k]=max(dp[i-1][j][k-1],dp[i][j][k]);34 dp[i][j][k]=max(dp[i][j-1][k-1],dp[i][j][k]);35 dp[i][j][k]=max(dp[i-1][j-1][k-1],dp[i][j][k]);36 dp[i][j][k]=max(dp[i][j][k-1],dp[i][j][k]);37 dp[i][j][k]+=abs(a[i][k-i+1]-a[j][k-j+1]);38 }39 }40 printf("%d",dp[n][n][2*n-1]);41 return 0;42 }