two-line DP, in Zheng Light when the two-line DP, which is multidimensional DP should be relatively simple, but that time complexity of the optimization always do not understand. Just attach the code, and then come back and add some explanations.
#include <stdio.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>#include<queue>#include<vector>#include<Set>#include<stack>#include<string>#include<sstream>#include<map>#include<cctype>#include<limits.h>using namespacestd;intmap1[ -][ -],m,n,dp[ -][ -];intGetmax () {inti,j,k; for(i=1; i<=m+n-1;i++)//In this case the sum of the number of rows running below { for(j=m-1; j>=0; j--)//several lines are routed down several lines. { for(k=m-1; k>j;k--)//start from a low line. { if(i>=k&&i>=j) Dp[j][k]=max (Max (dp[j][k],dp[j-1][k-1]), Max (dp[j-1][k],dp[j][k-1])) +map1[j][i-j]+map1[k][i-K]; } } } returndp[m-2][m-1];}intMain () {intN; scanf ("%d", &n);//Number of cases while(n--) {memset (MAP1,0,sizeof(MAP1)); Memset (DP,0,sizeof(DP)); scanf ("%d%d", &m,&n);//input length and width inti,j; for(i=0; i<m;i++) { for(j=0; j<n;j++) {scanf ("%d", &map1[i][j]);//input Diagram}} printf ("%d\n", Getmax ()); } return 0;}
Discover Treasures---dual-line DP