标签:
http://acm.hdu.edu.cn/showproblem.php?pid=5234
题意:一个小姑娘生日了,她妈妈把她领到了一个花园(n*m)里面,起始坐标在(1,1),每个坐标位置都有(a[i][j])的食物,她只能向左或向下走,并且她的胃口最大为K,最终到达(n, m)的位置,问你在满足题意的情况下,她最多能够吃多少食物。
分析:完全照着dp模板写的,没想到1A,百度了一下,没想到大神的做法和我一样。。。。
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algorithm> #include <map> #include <queue> #include <stack> #include <math.h> using namespace std; #define INF 0x3f3f3f3f const int maxn = 110; typedef long long LL; int a[maxn][maxn],dp[maxn][maxn][maxn]; int main() { int n, m, k; while(scanf("%d %d %d", &n, &m, &k)!=EOF) { memset(a, 0, sizeof(a)); for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) scanf("%d", &a[i][j]); memset(dp, 0, sizeof(dp)); for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { for(int p=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 { ///在取a[i][j]食物的情况下,判断左边的点与上边的点哪个值更大 int l=max(dp[i-1][j][p-a[i][j]], dp[i][j-1][p-a[i][j]])+a[i][j]; ///在不取a[i][j]食物的情况下,判断左边的点与上边的点哪个值更大 int r=max(dp[i-1][j][p], dp[i][j-1][p]); ///取四种情况下的最大值 dp[i][j][p]=max(l, r); } } } } printf("%d\n", dp[n][m][k]); } return 0; } /* 4 4 9 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 */
标签:
原文地址:http://www.cnblogs.com/daydayupacm/p/5777989.html