Discover Treasures time limit: +Ms | Memory Limit:65535KB Difficulty:5
-
-
Describe
-
Legend HMH There is a m*n maze in the great desert, with many treasures hidden in it. One day, Dr.kong found a maze of maps, he found in the maze everywhere there are treasures, the most precious treasures are hidden in the lower right corner, the labyrinth of import and export in the upper left corner. Of course, the pathways in the maze are not flat and there are traps everywhere. Dr.kong decided to let his robot go on an expedition.
But when the robot is moving from the upper-left corner to the lower-right corner, it will only go down or to the right. Go back from the lower right corner to the upper left corner, will only go up or left, and not go back. (i.e., one point at most once). And, of course, he took every treasure along the road.
Dr.kong wants his robot to make as many treasures as possible. Please write a program to help Dr.kong calculate how many treasures can be brought out by the most.
-
-
Input
-
-
First line: K indicates how many sets of test data are available.
Next to each set of test data:
Line 1th: M N
Line 2~m+1: Ai1 Ai2 ... AiN (I=1,....., m)
"Constraint conditions"
2≤k≤5 1≤m, n≤50 0≤aij≤100 (I=1,...., M; j=1,..., N)
All data are integers. There is a space between the data.
-
-
Output
-
-
for each set of test data, the output is one line: The number of treasures the robot has to carry the most value
-
-
Sample input
-
-
22 30 10 1010 10 803 30 3 92 8 55 7 100
-
-
Sample output
-
-
120134
-
-
Source
-
-
sixth session of Henan Province Program design Competition
-
-
Uploaded by
-
-
acm_ Zhao Minhao
The difference between this problem and the DP that we did in the past is that it's a one-time trip.
Join only go We can use dynamic programming equation Dp[i][j]=max (dp[i-1][j],dp[i][j-1]) +map[i][j].
And this problem went back and we can understand that two people at the same time from the upper left corner, but not the same way.
If two people do not go the same way then these two people must not be in the same column or row and because two people walk the exact same number of steps
So we can get the number of steps that another person walks by the number of steps a person walks
We can use a four-dimensional array to save
So this time the dynamic programming equation
dp[i][j][k][l]=max(max(dp[i-1][j][k-1][l],dp[i-1][j][k][l-1]),
29.
max(dp[i][j-1][k-1][l],dp[i][j-1][k][l-1]))+map[i][j]+map[k][l];
Attached code:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int map[55][55];int dp [55] [55] [55] [55];int Main () {int ncase;scanf ("%d", &ncase), while (ncase--) {int N,m;memset (dp,0,sizeof (DP)); memset (map,0, sizeof (map); scanf ("%d%d", &m,&n), for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) scanf ("%d", &map[i][ j]); for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) for (int k=i+1;k<=m;k++) {int l=i+j-k;if (l<0| | L>n) Break;dp[i][j][k][l]=max (Max (Dp[i-1][j][k-1][l],dp[i-1][j][k][l-1]), Max (dp[i][j-1][k-1][l],dp[i][j-1][k ][L-1]) +map[i][j]+map[k][l];} printf ("%d\n", Max (Max (dp[m][n-1][m-1][n],dp[m][n-1][m][n-1), Max (dp[m-1][n][m-1][n],dp[m-1][n][m][n-1])) +map[m ][n]);}}
Because the four-dimensional array occupies a very large space and because in this problem two people walk the exact same number of steps is i+j=k+l so we can be converted to 3-D by the number of steps
DP[K][I][J] where k is the current walking step I for the first person's row left J for the second person's line coordinates
And because I built the upper left corner of the coordinates for (a), so from the upper left to the lower right corner of the required minimum number of steps is M+n-2
The dynamic transfer equation at this time is
dp[k][i][j]=max(max(dp[k-1][i-1][j],dp[k-1][i-1][j-1]),
30.
max(dp[k-1][i][j],dp[k-1][i][j-1]))+map[i][k-i+2]+map[j][k-j+2];
AC Code:
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;int map[55][55];int dp [110] [55] [55];int Main () {int ncase;scanf ("%d", &ncase), while (ncase--) {int N,m;memset (dp,0,sizeof (DP)); memset (map,0, sizeof (map); scanf ("%d%d", &m,&n), for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) scanf ("%d", &map[i][ j]); int step=m+n-2;for (int i=1;i<=m;i++) for (int. j=i+1;j<=m;j++) for (int k=1;k<step;k++) {if (k+2>=i& &K+2>=J) {Dp[k][i][j]=max (max (dp[k-1][i-1][j],dp[k-1][i-1][j-1]), Max (Dp[k-1][i][j],dp[k-1][i][j-1])) +map I [K-i+2]+map[j][k-j+2];}} printf ("%d\n", Max (Dp[step-1][m][m-1],dp[step-1][m-1][m]) +map[m][n]);} return 0;}
Nyoj 712 Quest Treasure (double line DP sixth session of Henan Province Program design Competition)