Title Link: Http://codeforces.com/problemset/problem/429/B
Give you a matrix, a person from (1, 1)--(n, m), only down or to the right; a person from (n, 1) (1, m), only up or to the right. Must have a meeting point, the value of the meeting point can not be taken, ask two people can get the maximum path and how much?
DP[I][J]: Represents the maximum value from one point, preprocessing 4 DP maximums starting at (1,m) (n,1) (n,m) four points. Then enumerate all the points, but this point cannot be at the edge, consider that the enumeration points are not enough, and also consider enumerating the values of the point up or down.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = 1e3 +5;4 intDP1[MAXN][MAXN], DP2[MAXN][MAXN], DP3[MAXN][MAXN], DP4[MAXN][MAXN];5 intA[MAXN][MAXN];6 7 intMain ()8 {9 intN, M;Tenscanf"%d%d", &n, &m); One for(inti =1; I <= N; ++i) { A for(intj =1; J <= M; ++j) { -scanf"%d", &a[i][j]); - } the } - for(inti =1; I <= N; ++i) { - for(intj =1; J <= M; ++j) { -DP1[I][J] = max (Dp1[i-1][J], Dp1[i][j-1]) +A[i][j]; + } - } + for(inti = n; I >=1; --i) { A for(intj =1; J <= M; ++j) { atDP2[I][J] = max (dp2[i +1][J], Dp2[i][j-1]) +A[i][j]; - } - } - for(inti =1; I <= N; ++i) { - for(intj = m; J >=1; --j) { -DP3[I][J] = max (dp3[i][j +1], Dp3[i-1][J]) +A[i][j]; in } - } to for(inti = n; I >=1; --i) { + for(intj = m; J >=1; --j) { -DP4[I][J] = max (dp4[i +1][J], Dp4[i][j +1]) +A[i][j]; the } * } $ intres =0;Panax Notoginseng for(inti =2; I < n; ++i) { - for(intj =2; J < M; ++j) { theres = max (res, dp1[i][j-1] + dp2[i +1][J] + dp3[i-1][J] + dp4[i][j +1]); +res = max (res, dp1[i-1][J] + dp2[i][j-1] + dp3[i][j +1] + dp4[i +1][j]); A } the } +printf"%d\n", res); - return 0; $}
Codeforces Round #245 (Div. 1) B. Working out (simple DP)