Happy birthday hdu5234(三維dp)

來源:互聯網
上載者:User

標籤:

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 0x3f3f3f3fconst 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 91 2 3 41 2 3 41 2 3 41 2 3 4*/
View Code

 

Happy birthday hdu5234(三維dp)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.