Blue Bridge Cup pass notes dp

Problem Description
  Xiaoyuan and Xiaoxuan are good friends and classmates, and they always have endless topics together. In a quality development activity, classmates arranged a matrix of m rows and n columns, and Xiaoyuan and Xiaoxuan were arranged at the two ends of the diagonal of the matrix, so they could not directly talk. Fortunately, they can communicate through paper slips. The paper should be passed to the other party through many students. Xiaoyuan sat at the upper left corner of the matrix at coordinates (1,1), and Xiaoxuan sat at the lower right corner of the matrix at coordinates (m, n). The paper from Xiaoyuan to Xiaoxuan can only be passed down or to the right, and the paper from Xiaoxuan to Xiaoyuan can only be passed up or to the left.
  During the event, Xiaoyuan hoped to pass a note to Xiaoxuan, and hoped that Xiaoxuan would reply to him. Every classmate in the class can help them pass, but only for them once, that is to say, if this person helps when Xiaoyuan handed Xiaoxuan notes, then when Xiaoxuan hands Xiaoyuan Help again. vice versa.
  There is one more thing to note. The favorability of each student in the class is high or low (note: the degree of kindness of Xiaoyuan and Xiaoxuan is not defined, and it is represented by 0 when entering). To indicate, the larger the number, the more kind. Xiaoyuan and Xiaoxuan hope to find students with good intentions as much as possible to help spread the paper, that is, find two transfer paths back and forth, so that the kindness of the students on these two paths is only the greatest. Now, please help Xiaoyuan and Xiaoxuan find these two paths.
Input format
  Enter two integers m and n separated by spaces in the first row, indicating that there are m rows and n columns in the class (1 <= m, n <= 50).
  The next m rows are an m * n matrix. The integers in the i-th row and j-column of the matrix represent the kindness of the students sitting in the i-th row and j-column. The n integers in each line are separated by spaces.
Output format
  A line is output, containing an integer, which represents the maximum value of the sum of the kindness of the students who participated in passing the note back and forth.
Sample input
3 3
0 3 9
2 8 5
5 7 0
Sample output
34
Data size and agreement
  30% data satisfies: 1 <= m, n <= 10
  100% data satisfies: 1 <= m, n <= 50
The first time I saw the four-dimensional dp problem, it was a long knowledge.
Reference from https://www.cnblogs.com/cao-lei/p/7236061.html
This question still needs some basic dp skills to be easy to understand.
The original blogger started with the four-dimensional array, which is the easiest to understand, optimized to a three-dimensional array, and then optimized to a two-dimensional array.
Following the original blogger's ideas, it is easy to understand how 4D is done, and then took a moment to understand how to optimize to 3D.
But I submitted the two-dimensional array code of the original blogger and found that it was only 70 points, but now I can't find out what is wrong.
However, the solution of the three-dimensional array, the time complexity O (n ^ 3), is enough to solve the case of n = 100. For this problem, n = 50. For the Blue Bridge Cup, the three-dimensional solution is enough.
Good questions to be reviewed.
Four-dimensional array solution:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int mp[55][55];     //好心程度 | 权值
 4 int dp[55][55][55][55];
 5 int maxPath(int m, int n) {
 6     for (int x1 = 1; x1 <= m; x1++) {
 7         for (int y1 = 1; y1 <= n; y1++) {
 8             for (int x2 = 1; x2 <= m; x2++) {
 9                 for (int y2 = 1; y2 <= n; y2++) {
10                     if ((x1 < m || y1 < n) && x1 == x2 && y1 == y2) {
11                         continue;
12                     }
13                     dp[x1][y1][x2][y2] = max(max(dp[x1 - 1][y1][x2 - 1][y2], dp[x1 - 1][y1][x2][y2 - 1]), 
14                                               max(dp[x1][y1 - 1][x2 - 1][y2], dp[x1][y1 - 1][x2][y2 - 1]))
15                                              + mp[x1][y1] + mp[x2][y2];
16                 }
17             }
18         }
19     }
20     return dp[m][n][m][n]; 
21 }
22 int main() {
23     int m, n;
24     cin >> m >> n;
25     for (int i = 1;i <= m; i++) {
26         for (int j = 1;j <= n; j++) {
27             cin >> mp[i][j];
28         }
29     }
30     int ans = maxPath(m, n);
31     cout << ans << endl;
32     return 0;
33 }
三维数组解法:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int mp[55][55]; //好心程度 | 权值
 4 int dp[55 + 55][55][55];
 5 int maxPath(int m, int n) {
 6     for (int k = 1; k <= m + n - 3; k++) {
 7         for (int x1 = 0; x1 <= k; x1++) {
 8             for (int x2 = 0; x2 <= k; x2++) {
 9                 if (x1 == x2) { //x1 == x2 相当于(x1 == x2 && y1 = y2) 
10                     continue;
11                 }
12                 dp[k][x1][x2] = max(max(dp[k - 1][x1][x2], dp[k - 1][x1 - 1][x2 - 1]),
13                                     max(dp[k - 1][x1 - 1][x2], dp[k - 1][x1][x2 - 1]))
14                                     + map[x1][k - x1] + map[x2][k - x2];
15             }
16         }
17     }
18     return dp[m + n - 3][m - 1][m - 2];
19 }
20 int main() {
21     int m, n;
22     cin >> m >> n;    
23     for (int i = 0; i < m; i++) {
24         for (int j = 0; j < n; j++) {
25             cin >> mp[i][j];
26         }
27     }
28     int ans = maxPath(m, n);
29     cout << ans << endl;
30     return 0;
31 }

Guess you like

Origin www.cnblogs.com/fx1998/p/12697293.html