DP[I][J][K][L]
Represents the maximum level of enthusiasm from (I,J) to (from) to (k,l) at the same time.
(= = three-dimensional optimization has time to engage in. )
Note that there is a place that is not the same as other people, I am the judge if the end of the repetition, the direct subtraction of the point of kindness, indicating that there is a pass through the position of the person's kindness is 0;
#include <iostream>using namespacestd;intmap[ -][ -]={0},dp[ -][ -][ -][ -]={0};intMaxintAintb) {returnA>b?a:b;}intMainintargcChar Const*argv[]) { intn,m; CIN>>n>>m; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) Cin>>Map[i][j]; for(intI=1; i<=n;i++) for(intj=1; j<=m;j++) for(intk=1; k<=n;k++) for(intL=1; l<=m;l++) {Dp[i][j][k][l]=Max (//each route is derived from the previous two pointsMax (dp[i-1][j][k-1][l],dp[i-1][j][k][l-1]), Max (Dp[i][j-1][k-1][l],dp[i][j-1][k][l-1]) ) + map[i][j]+Map[k][l]; if(i==k&&j==l)//If you pass the same point, subtract a kindness value, because that road of repetition can be thought of as 0 kindness.dp[i][j][k][l]-=Map[i][j]; } cout<<dp[n][m][n][m]<<Endl; return 0;}
"Algorithmic Learning Notes" 77. Dynamic planning of two-line chessboard SJTU OJ 1263 paper to go to the paper