Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2845
Test instructions: give you a n*m matrix, each location has a certain number of beans, if you go to map[x][y] position on the beans, then map[i-1][] line and map[i+1][] line, and map[i][j-1] and map[i][j+1] location of the beans can not be taken.
Analysis: Water DP, the maximum value of each row is calculated first, the maximum value of n rows DP results. Dp[i][1] represents the first I and takes the maximum value reached by the first I, Dp[i][0] represents the first I and does not take the maximum value reached by the first I.
State transition equation: Dp[i][0]=max (dp[i-1][0],dp[i-1][1]), dp[i][1]=dp[i-1][0]+b[i];
#include <cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#include<cstdlib>#include<stack>#include<vector>#include<Set>#include<map>#defineLL Long Long#defineMoD 1000000007#defineINF 0x3f3f3f3f#defineN 200010using namespacestd;intA[n];intmx[n],dp[n][2];intn,m;intSolveintB[],intsum) { for(intI=1; i<=sum; i++) {dp[i][1]=dp[i-1][0]+B[i]; dp[i][0]=max (dp[i-1][0],dp[i-1][1]); } returnMax (dp[sum][0],dp[sum][1]);}intMain () { while(SCANF ("%d%d", &n,&m) >0) { for(intI=1; i<=n; i++) { for(intj=1; j<=m; J + +) {scanf ("%d",&A[j]); } Mx[i]=solve (a,m); } intans=solve (mx,n); printf ("%d\n", ans); }}View Code
hdu2845 (DP)