http://acm.hdu.edu.cn/showproblem.php?pid=5234
Test instructions: A little girl's birthday, her mother led her to a garden (n*m) inside, the starting coordinates at (a), each coordinate position has (a[i][j]) food, she can only go left or down, and her appetite Max K, finally reached (n, m) position, asked you in the case of satisfying test instructions, The maximum amount of food she can eat.
Analysis: Completely according to the DP template written, did not expect 1 A, Baidu a bit, did not think the great God's practice and I like ....
#include <iostream>#include<stdio.h>#include<string.h>#include<string>#include<vector>#include<algorithm>#include<map>#include<queue>#include<stack>#include<math.h>using namespacestd;#defineINF 0x3f3f3f3fConst intMAXN = the; typedefLong LongLL;intA[MAXN][MAXN],DP[MAXN][MAXN][MAXN];intMain () {intN, M, K; while(SCANF (" %d%d%d", &n, &m, &k)! =EOF) {memset (A,0,sizeof(a)); for(intI=1; i<=n; i++) for(intj=1; j<=m; J + +) scanf ("%d", &A[i][j]); Memset (DP,0,sizeof(DP)); for(intI=1; i<=n; i++) { for(intj=1; j<=m; J + +) { for(intp=0; p<=k; p++) { if(p<A[i][j]) dp[i][j][p]=max (dp[i-1][j][p], dp[i][j-1][p]); Else { ///in the case of taking a[i][j] food, judging the point on the left is larger than the point above intL=max (dp[i-1][J][P-A[I][J]], dp[i][j-1][P-A[I][J]]) +A[i][j]; ///If you do not take a[i][j] food, judging the point on the left and the top of the point which is larger intR=max (dp[i-1][j][p], dp[i][j-1][p]); ///maximum value in four casesdp[i][j][p]=Max (L, R); } }}} printf ("%d\n", Dp[n][m][k]); } return 0;}/*4 4 2 3 (2 3, 2 3) 2 3 4*/View Code
Happy Birthday hdu5234 (three-dimensional DP)