It was a long-time problem.
Test instructions: From the upper left-hand corner of the grid select a path to the upper right corner and then back to the upper left corner, and two paths in addition to the start and end point cannot have coincident points. Ask what's the biggest and what's in the grid.
State Design : We can think of two paths starting from the top left and then reaching the lower right corner at the same time. It is easy to see that all possible squares of the first k stages constitute a slash and satisfy x1 + y1 = x2 + y2 = k + 1
DP[K][X1][X2] indicates the maximum value (Y2 y1 based on the equivalent of the above) can be reached (x1, y1) (x2, y2) of phase k, if X1 = x2 means that two paths converge at one point.
state transition Equation :
When solving the K-phase optimal solution, the state of the k-1 phase (x1 ', X2 ') is enumerated first and then extended in four directions (x1, x2)
DP[K][X1][X2] = max{dp[k-1][x1 '][x2 ') + num[x1][y1] | (x1=x2), dp[k-1][x1 '][x2 '] + num[x1][y1] + Num[x2][y2] | (X1≠X2)}
In the cycle of the process may occur from a point to extend the next point of the case, that is, two paths may coincide, but it does not matter, because the lattice inside are positive, so the optimal solution must be two non-coincident path
optimization :
Since two paths are arbitrary, it is advisable to specify that the first road is below the second (or the same horizontal line) and that the time is optimized from 92MS to 62MS
The transition of the phase K State depends only on the state of the k-1 stage, which can be used as a scrolling array (because the original space is not large, so the code does not implement a scrolling array)
1 //#define LOCAL2#include <iostream>3#include <cstdio>4#include <cstring>5 using namespacestd;6 7 intdp[ -][ *][ *], num[ *][ *], N;8 9 BOOLIslegal (intXinty)Ten { One return(x>=1&& x<=n && y>=1&& y<=n); A } - - intMainvoid) the { - #ifdef LOCAL -Freopen ("2686in.txt","R", stdin); - #endif + - intI, J, K, D1, D2, X1, x2, y1, y2; + while(SCANF ("%d", &n) = =1) A { atMemset (DP,0,sizeof(DP)); - for(i =1; I <= N; ++i) - for(j =1; J <= N; ++j) -scanf"%d", &num[i][j]); - -dp[1][1][1] = num[1][1]; in for(k =2; K <=2*n-1; ++k) - { to for(i =1; I <= K-1; ++i) + for(j =1; J <= I; ++j) -{//enumerates the state of two paths at k-1 step the for(D1 =0; D1 <=1; ++D1) * for(D2 =0; D2 <=1; ++D2) ${//A total of four extension directionPanax Notoginsengx1 = i + d1, y1 = k +1-X1; -x2 = j + D2, y2 = k +1-x2; the if(Islegal (x1, y1) &&Islegal (x2, y2)) + { A if(X1 = =x2) theDP[K][X1][X2] = max (dp[k][x1][x2], dp[k-1][I][J] +num[x1][y1]); + Else -DP[K][X1][X2] = max (dp[k][x1][x2], dp[k-1][I][J] + num[x1][y1] +Num[x2][y2]); $ } $ } - } - } theprintf"%d\n", dp[2*n-1][n][n]); - }Wuyi return 0; the}
code June
HDU 2686 (Dual-threaded) Matrix