The intersection of a 1000*1000 graph is one, and how to draw a picture at the same point can be found in two cases, so the first thing that comes to mind is that the intersection can be enumerated violently, then push forward by the intersection point. After the intersection, the two people continue to move forward to their own destination. Therefore, we can enumerate and roll out the maximum values of each vertex to the four vertices of the graph, then we can obtain the optimal one based on the intersection conditions.
Int MP [1000 + 55] [1000 + 55]; int dp1 [1000 + 55] [1000 + 55], dp2 [1000 + 55] [1000 + 55], dp3 [1000 + 55] [1000 + 55], dp4 [1000 + 55] [1000 + 55]; int n, m; void Init () {memset (MP, 0, sizeof (MP); memset (dp1, 0, sizeof (dp1); memset (dp2, 0, sizeof (dp2); memset (dp3, 0, sizeof (dp3); memset (dp4, 0, sizeof (dp4);} bool input () {While (CIN> N> m) {for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) scanf ("% d ", & amp; MP [I] [J]); Return false;} return true;} void CAL () {for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) dp1 [I] [J] = MP [I] [J] + max (dp1 [I-1] [J], dp1 [I] [J-1]); // recursive the current vertex to the top left vertex with the largest value for (INT I = 1; I <= N; I ++) for (Int J = m; j> 0; j --) dp2 [I] [J] = MP [I] [J] + max (dp2 [I-1] [J], dp2 [I] [J + 1]); // top right... for (INT I = N; I> 0; I --) for (Int J = 1; j <= m; j ++) dp3 [I] [J] = MP [I] [J] + max (dp3 [I + 1] [J], dp3 [I] [J-1]); // bottom left... for (INT I = N; I> 0; I --) for (Int J = m; j> 0; j --) dp4 [I] [J] = MP [I] [J] + max (dp4 [I + 1] [J], dp4 [I] [J + 1]); // ...int ans = 0 at the bottom of the right;/* indicates the point of the violent enumeration. The point of intersection is */For (INT I = 2; I <n; I ++) {/* it is impossible to meet each other in the first column. After the encounter, the upper and lower sides can only walk to the right. Similarly, the last column */For (Int J = 2; j <m; j ++) {ans = max (ANS, dp1 [I] [J-1] + dp2 [I-1] [J] + dp3 [I + 1] [J] + dp4 [I] [J + 1]); // There are two kinds of intersection conditions. ans = max (ANS, dp1 [I-1] [J] + dp2 [I] [J + 1] + dp3 [I] [J-1] + dp4 [I + 1] [J]); // note that the current intersection value is not included} cout <ans <Endl;} void output () {} int main () {While (true) {Init (); If (input () return 0; CAL (); output ();} return 0 ;}
Codeforces round #245 (Div. 1) B recurrence DP