Description:
Square plot with N * n (n <= 10, we fill in some squares in a positive integer, and other squares in a number 0. As shown in (see example ):
A person starts from the point in the upper left corner of the graph and can walk down or right until the B point in the lower right corner. On the way he walked, he could take away the number from the square (the square after the square is removed will become a number 0 ).
This person takes two steps from A.M. To a.m. and tries to find two such paths to maximize the sum of the obtained numbers.
Ideas:
(1) DP, 4-dimensional, DP [I1, J1, I2, J2] indicates the maximum value obtained when two paths go to (I1, J1) and (I2, J2) respectively.
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 int a[11][11]; 7 int dp[11][11][11][11]; 8 const int INF = 999999999; 9 10 int operDp(int n)11 {12 int i1, j1, i2, j2;13 memset(dp,0,sizeof(dp));14 for(i1 = 1; i1 <= n; i1++)15 for(j1 = 1; j1 <= n; j1++)16 for(i2 = 1; i2 <= n; i2++)17 for(j2 = 1; j2 <= n; j2++)18 {19 int tmp = -INF;20 tmp = max(tmp, dp[i1-1][j1][i2-1][j2]);21 tmp = max(tmp, dp[i1-1][j1][i2][j2-1]);22 tmp = max(tmp, dp[i1][j1-1][i2-1][j2]);23 tmp = max(tmp, dp[i1][j1-1][i2][j2-1]);24 if(i1 == i2 && j1 == j2)25 dp[i1][j1][i2][j2] = tmp + a[i1][j1];26 else27 dp[i1][j1][i2][j2] = tmp + a[i1][j1] + a[i2][j2];28 }29 return dp[n][n][n][n];30 }31 32 int main()33 {34 int n, r, c, v;35 while(scanf("%d",&n) != EOF)36 {37 memset(a,0,sizeof(a));38 while(true)39 {40 scanf("%d%d%d",&r,&c,&v);41 if(!r && !c && !v) break;42 a[r][c] = v;43 }44 printf("%d\n",operDp(n));45 }46 return 0;47 }
(2) reduce the dimension of the DP state
DP [k] [I] [J] indicates that step K is taken. Step I is taken from the first to the right, and step J is taken from the second to the right. The maximum value of K is n, n to the right, that is, 2 * n
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 int a[11][11]; 7 int dp[21][11][11]; 8 const int INF = 999999999; 9 10 int operDp(int n)11 {12 int i, j, k;13 memset(dp,0,sizeof(dp));14 for(k = 1; k <= 2 * n; k++)15 for(i = 1; i <= k; i++)16 for(j = 1; j <= k; j++)17 {18 int tmp = -INF;19 tmp = max(tmp, dp[k-1][i-1][j-1]);20 tmp = max(tmp, dp[k-1][i-1][j]);21 tmp = max(tmp, dp[k-1][i][j-1]);22 tmp = max(tmp, dp[k-1][i][j]);23 if(i == j) dp[k][i][j] = tmp + a[k-i+1][i];24 else dp[k][i][j] = tmp + a[k-i+1][i] + a[k-j+1][j];25 }26 return dp[2*n][n][n];27 }28 29 int main()30 {31 int n, r, c, v;32 while(scanf("%d",&n) != EOF)33 {34 memset(a,0,sizeof(a));35 while(true)36 {37 scanf("%d%d%d",&r,&c,&v);38 if(!r && !c && !v) break;39 a[r][c] = v;40 }41 printf("%d\n",operDp(n));42 }43 return 0;44 }