Http://acm.nyist.net/JudgeOnline/problem.php? PID = 61
Http://acm.nyist.net/JudgeOnline/problem.php? PID = 1, 712
This is a dual-process DP problem. First, assume that the starting point is a and the end point is B. Then, based on the conditions given in the question, the dynamic transfer equation of a-> B can be introduced as DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1]).
+ A [I] [J]; because B is available in the same way, the question means a-> B and B-> A. We can assume that at the same time starting from, two different paths are obtained, which has the same effect. Therefore, we can obtain a dynamic transfer equation.
DP [I] [J] [p] [Q] = max (DP [I-1] [J] [PM] [Q], DP [I-1] [J] [p] [Q], DP [I] [J-1] P-1] [Q], DP [I] [J-1] [p] [q-1]) because only one step can be moved at a time, that is
I + 1 or J + 1, then I + J is the number of moving steps. Because I started to move from vertex A, after the same number of steps, I + J = p + q will certainly be obtained.
Note that this is similar to nyoj 61, but the final endpoint value should also be included in the details, the value obtained from the above dynamic equation does not contain two values of A and B. Because a is the starting point, its value is generally 0. Therefore, the final result is
Int sum = max (DP [M-1] [N] m-1] [N], DP [M-1] [N] [m] [n-1], DP [m] [n-1] [M-1] [N], DP [m] [n-1] [m] [n-1]) + A [m] [N];
Nyist 61 code
#include <iostream>#include <cstring>using namespace std;int a[55][55],dp[55][55][55][55];int main(int argc, char *argv[]){int t,n,m,i,j,p,q,ans;cin>>t;while(t--){cin>>n>>m;for(i=1;i<=n;i++)for(j=1;j<=m;j++)cin>>a[i][j];memset(dp,0,sizeof(dp));for(i=1;i<=n;i++)for(j=1;j<=m;j++)for(p=i+1;p<=n;p++){q=i+j-p;if(q<=0) continue;dp[i][j][p][q] = max(max(dp[i-1][j][p-1][q],dp[i][j-1][p][q-1]), max(dp[i-1][j][p][q-1],dp[i][j-1][p-1][q])) + a[i][j] + a[p][q];}ans=max(max(dp[n-1][m][n-1][m],dp[n-1][m][n][m-1]), max(dp[n][m-1][n-1][m],dp[n][m-1][n][m-1])); cout<<ans<<endl;}return 0;}
Nyist 712 code:
#include <iostream>#include <cstring>using namespace std;int a[55][55],dp[55][55][55][55];int main(int argc, char *argv[]){int t,n,m,i,j,p,q,ans;cin>>t;while(t--){cin>>n>>m;for(i=1;i<=n;i++)for(j=1;j<=m;j++)cin>>a[i][j];memset(dp,0,sizeof(dp));for(i=1;i<=n;i++)for(j=1;j<=m;j++)for(p=i+1;p<=n;p++){q=i+j-p;if(q<=0) continue;dp[i][j][p][q] = max(max(dp[i-1][j][p-1][q],dp[i][j-1][p][q-1]), max(dp[i-1][j][p][q-1],dp[i][j-1][p-1][q])) + a[i][j] + a[p][q];}ans=max(max(dp[n-1][m][n-1][m],dp[n-1][m][n][m-1]), max(dp[n][m-1][n-1][m],dp[n][m-1][n][m-1])); cout<<ans+a[n][m]<<endl;}return 0;}