Baker Vai LightOJ, bakervailightoj
Meaning: similar to passing a piece of paper
Method:
The operation he requested (one person goes back and forth) is converted to taking two people at the same time, except that the start and end positions can only go different ways, and the score and the maximum value can be obtained.
At first, the state to be defined is the x (ROW) and y (column) coordinates of two people. In this way, both the time and space are $ O (n ^ 4) $, and the time and space are exceeded. Therefore, optimization is required. Note that the total number of steps from the start point to the end point must be the same, and the y coordinate of the person can be introduced based on the number of steps taken by each person and the x coordinate. You can only record the number of steps and the x coordinate of two people as the State. In this way, you can optimize the time/space to $ O (n ^ 3) $. (Space can be optimized by scrolling arrays, but it is sufficient if optimization is not performed)
Number of errors: 2
Cause:
1. 27 lines incorrectly write ans [0] [1] [1] = 1
2. The first dimension of ans has the same size (110) as that of the second and third dimensions, which requires 2 times the second and third dimensions.
3. C ++ 11 min ({...,...}) is used to cause CE
1 # include <cstdio> 2 # include <cstring> 3 # include <algorithm> 4 using namespace std; 5 int T, TT, m, n, maxans; 6 int ans [220] [110] [110]; 7 int a [110] [110]; 8 int max (int a, int B, int c, int d) 9 {10 int ans = a; 11 if (B> ans) ans = B; 12 if (c> ans) ans = c; 13 if (d> ans) ans = d; 14 return ans; 15} 16 int main () 17 {18 int I, j, j1, j2; 19 scanf ("% d", & T ); 20 for (TT = 1; TT <= T; TT ++) 21 {22 scanf ("% d", & m, & n ); 23 for (I = 1; I <= m; I ++) 2 4 for (j = 1; j <= n; j ++) 25 scanf ("% d", & a [I] [j]); 26 memset (ans, 0, sizeof (ans); 27 ans [0] [1] [1] = a [1] [1]; // the start point must be specially processed, two people in the same location 28 maxans = 0; 29 for (I = 1; I <= m + n-3; I ++) 30 for (j1 = max (1, i-n + 2); j1 <= min (I + 1, m); j1 ++) // max and min are based on the export data range of 31 for (j2 = max (1, I-n + 2); j2 <= min (I + 1, m ); j2 ++) 32 if (j1! = J2) // ensure that two people are not in the same row, that is, not in the same location 33 ans [I] [j1] [j2] = max (ans [I-1] [j1] [j2], ans [I-1] [j1-1] [j2], ans [I-1] [j1] [j2-1], ans [I-1] [j1-1] [j2-1]) + a [j1] [i-j1 + 2] + a [j2] [i-j2 + 2]; 34 I = m + N-2; // special treatment for endpoints, because two people at the end can go to the same position 35 for (j1 = max (1, I-n + 2); j1 <= min (I + 1, m); j1 ++) 36 for (j2 = max (1, I-n + 2); j2 <= min (I + 1, m); j2 ++) 37 ans [I] [j1] [j2] = max (ans [I-1] [j1] [j2], ans [I-1] [j1-1] [j2], ans [I-1] [j1] [j2-1], ans [I-1] [j1-1] [j2-1]) + a [j1] [i-j1 + 2] + a [j2] [i-j2 + 2]; 38 for (j1 = 1; j1 <= m; j1 + +) 39 for (j2 = 1; j2 <= m; j2 ++) 40 maxans = max (maxans, ans [m + N-2] [j1] [j2]); 41 printf ("Case % d: % d \ n", TT, maxans-a [m] [n]); 42} 43 return 0; 44}