HDU 5234 Happy birthday 类背包dp

本文介绍了一种寻路算法的应用场景——蛋糕花园中寻找获得最大蛋糕重量的路径。主人公Gorwin在一个由网格组成的花园中从左上角走到右下角,每一步可以选择向右或向下移动,并可以选择吃掉所在网格的蛋糕。算法需考虑路径选择和携带蛋糕的最大重量限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
Today is Gorwin’s birthday. So her mother want to realize her a wish. Gorwin says that she wants to eat many cakes. Thus, her mother takes her to a cake garden. 

The garden is splited into n*m grids. In each grids, there is a cake. The weight of cake in the i-th row j-th column is  wij  kilos, Gorwin starts from the top-left(1,1) grid of the garden and walk to the bottom-right(n,m) grid. In each step Gorwin can go to right or down, i.e when Gorwin stands in (i,j), then she can go to (i+1,j) or (i,j+1) (However, she can not go out of the garden). 

When Gorwin reachs a grid, she can eat up the cake in that grid or just leave it alone. However she can’t eat part of the cake. But Gorwin’s belly is not very large, so she can eat at most K kilos cake. Now, Gorwin has stood in the top-left grid and look at the map of the garden, she want to find a route which can lead her to eat most cake. But the map is so complicated. So she wants you to help her.
 

Input
Multiple test cases (about 15), every case gives n, m, K in a single line.

In the next n lines, the i-th line contains m integers  wi1,wi2,wi3,wim  which describes the weight of cakes in the i-th row

Please process to the end of file.

[Technical Specification]

All inputs are integers.

1<=n,m,K<=100

1<= wij <=100
 

Output
For each case, output an integer in an single line indicates the maximum weight of cake Gorwin can eat.
 

Sample Input
  
1 1 2 3 2 3 100 1 2 3 4 5 6
 

Sample Output
  
0 16
Hint
In the first case, Gorwin can’t eat part of cake, so she can’t eat any cake. In the second case, Gorwin walks though below route (1,1)->(2,1)->(2,2)->(2,3). When she passes a grid, she eats up the cake in that grid. Thus the total amount cake she eats is 1+4+5+6=16.
 

Source


dp[i][j][k] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j-1][k-w[i][j]]+w[i][j],dp[i-1][j][k-w[i][j]]+w[i][j]);


CODE

#include <bits/stdc++.h>
using namespace std;
const int N = 100+10;
int n,m,k;
int w[N][N];
int dp[N][N][N];

int main(void)
{
    while(scanf("%d%d%d",&n,&m,&k) != EOF){
        memset(dp,0,sizeof dp);
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= m;j++) scanf("%d",&w[i][j]);
        for(int i = 1;i <= m;i++){   ///预处理第一排
            for(int j = 1;j <= k;j++){
                dp[1][i][j] = dp[1][i-1][j];
                if(j >= w[1][i])
                    dp[1][i][j] = max(dp[1][i][j],dp[1][i-1][j-w[1][i]]+w[1][i]);
            }
        }
        for(int i = 2;i <= n;i++){
            for(int j = 1;j <= m;j++){
                for(int t = 1;t <= k;t++){
                    dp[i][j][t] = max(dp[i-1][j][t],dp[i][j-1][t]);
                    int tmp;
                    if(t >= w[i][j]){
                        tmp = max(dp[i-1][j][t-w[i][j]]+w[i][j],dp[i][j-1][t-w[i][j]]+w[i][j]);
                        dp[i][j][t] = max(dp[i][j][t],tmp);
                    }
                }
            }
        }
        int ans = 0;
        for(int i = 0;i <= k;i++){
            ans = max(ans,dp[n][m][i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值