码迷,mamicode.com
首页 > 移动开发 > 详细

Happy birthday hdu5234(三维dp)

时间:2016-08-16 23:50:01      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

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
*/
View Code

 

Happy birthday hdu5234(三维dp)

标签:

原文地址:http://www.cnblogs.com/daydayupacm/p/5777989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!