Back and forth can be seen as two disjoint paths from (N,n)
Dp[k][x1][y1][x2][y2] = max (dp[k-1][x1-1][y1][x2-1][y2],dp[k-1][x1-1][y1][x2][y2-1],dp[k-1][x1][y1-1][x2-1][y2 ],DP[K-1][X1][Y1-1][X2][Y2-1])
K is the number of steps
K related to X1,y1, can be reduced to three-dimensional
//Multi-decision DP//Dp[k][x1][y1][x2][y2] = max (dp[k-1][x1-1][y1][x2-1][y2],dp[k-1][x1-1][y1][x2][y2-1],dp[k-1][x1][y1-1][x2-1][ Y2],DP[K-1][X1][Y1-1][X2][Y2-1])//K is the number of steps//K related to x1,y1, can be reduced to three-dimensional#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<string>#include<stack>#include<queue>using namespacestd;Const intINF = (1<< to)-1;Const intMAXN = 3e1+Ten;intdp[2*MAXN] [MAXN] [MAXN];intA[MAXN][MAXN];intt1[2],t2[2];intMain () {intN; while(~SCANF ("%d",&N)) { for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++) {scanf ("%d",&A[i][j]); }} memset (DP,0,sizeof(DP)); //dp[0][1][1] = a[1][1]; for(intI=1; i<=2*n-3; i++){ for(intj=1; j<=i+1&&j<=n-1; j + +) {//X1 Meet if(i+2-j>n)Continue;//Y1 Meet for(intk=j+1; k<=i+1&&k<=n;k++){ if(i+2-k>n-1)Continue; t1[0] =J; t1[1] = J1; t2[0] =K; t2[1] = k1; for(intL=0;l<2; l++){ for(intR=0;r<2; r++){ if(T1[l]==t2[r])Continue; DP[I][J][K]= Max (dp[i][j][k],dp[i-1][t1[l]][t2[r]]); }} Dp[i][j][k]+ = a[j][i+2-j]+a[k][i+2-K]; /*cout<<i<< "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl; cout<< "Debug" <<endl;*/} }} dp[2*n-2][n][n] = dp[2*n-3][n-1][n] +A[n][n]; cout<<dp[2*n-2][n][n]+a[1][1]<<Endl; } //cout << "Hello world!" << Endl; return 0;}View Code
HDU2686 Multi-decision DP