hdu---5234---Happy birthday

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 ${w_{ij}}$ 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 ${w_{i1}},{w_{i{\rm{2}}}},{w_{i3}}, \cdots {w_{im}}$ 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<=${w_{ij}}$<=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.

Mean:

给一个n*m的矩阵,每个点是一个蛋糕的的重量,

然后Gorwin只能向右,向下走,求在不超过K千克的情况下,Gorwin最终能吃得最大重量的蛋糕.

anlayse:

类似背包DP;

状态转移方程:dp[i][j][k]----在i,j位置时,最大容量为k时的最大值;

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
typedef long long LL;

const int maxn=109;
const int INF=0x3f3f3f3f;
const int mod=2009;

int dp[maxn][maxn][maxn];
int num[maxn][maxn];

int main()
{
    int n, m, k;
    while(~scanf("%d %d %d", &n, &m, &k))
    {
        for(int i=1; i<=n; i++)
            for(int j=1; j<=m; j++)
                scanf("%d", &num[i][j]);

        memset(dp, 0, sizeof(dp));
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                for(int z=k; z>=num[i][j]; z--)
                {
                    int x=max(dp[i-1][j][z], dp[i][j-1][z]);
                    int y=max(dp[i-1][j][z-num[i][j]]+num[i][j], dp[i][j-1][z-num[i][j]]+num[i][j]);
                    dp[i][j][z]=max(x, y);
                }
            }
        }
        printf("%d\n", dp[n][m][k]);
    }
    return 0;
}
时间: 2024-10-05 09:48:25

hdu---5234---Happy birthday的相关文章

HDU 5234 Happy birthday --- 三维01背包

HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置(i,j)有一个容量为k的背包所能获得的最大价值 决策:a[i][j]处的数是否选取 不选取: dp[i][j][k]= max(dp[i-1][j][k], dp[i][j-1][k]) 选取:首先要求k >=a[i][j],那么dp[i][j][k] = max(dp[i-1][j][k-w[i

hdu 5234 Happy birthday【动态规划】

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5234 题意:给出一个n*m的矩阵和一个整数k,要求从左上角开始只能往右 或者往左开始遍历,在途中可以选择加当前位置的数或者不加当前位 置的数,求最终加的数的和小于k的最大值. 分析:这道题一看就是一般的背包,但是没研究过背包和dp,只是凭 这感觉写了状态转移方程,wa了几次A了,感觉dp是个很好的东西,只 是自己图论还没学好就从来没弄过这个,其实还是可以学学的. 首先先说一下背包九讲里面的第一个内容

hdu 5234 Happy birthday 背包 dp

Happy birthday Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5234 Description 今天是Gorwin的生日.所以她的妈妈要实现她的一个愿望.Gorwin说她想吃很多蛋糕.所以他妈妈带她来到了蛋糕园. 这个园子被分成了n*m个方格子.在每一个格子里面,有一个蛋糕.第i行,第j列的格子中有一个重量为wij千克的蛋糕,Gorwin从左上角(1,1

HDU 5234 Happy birthday 01背包

题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5234 bc:http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=585&pid=1003 题解: 由于数据比较小,所以可以转化为判定性问题,即: dp[i][j][kk]表示走到i,j这一点时吃了kk重的蛋糕,转移方程只要考虑这一点的蛋糕吃和不吃两种情况(01背包) 代码: 1 #include<ios

HDU 5234 Happy birthday 动态规划(三维数组)

题目大意:过生日,有一个N*M的表格,每个位置都有一块一定重量的蛋糕你可以选择吃完或者不吃,从(1,1)走到(n,m),每次只能向右走或向下走,最多能吃k重量的蛋糕.问你最多能吃多少蛋糕. 题目思路:之前的01背包我们都是用一维数组v[]来储存的,但这次要用二维数组Map[i][j]储存一个点的价值,当前点由Map[i][j-1]或Map[i-1][j]走到. 状态转移方程式:dp[i][j][q]=max(dp[i][j][q],Map[i][j]+max(dp[i][j-1][q-Map[i

HDU 5234 DP背包

题意:给一个n*m的矩阵,每个点是一个蛋糕的的重量,然后小明只能向右,向下走,求在不超过K千克的情况下,小明最终能吃得最大重量的蛋糕. 思路:类似背包DP: 状态转移方程:dp[i][j][k]----在i,j位置时,最大容量为k时的最大值: 做背包循环一般从1开始,因为需要坐标-1的情况,从0开始需要特判,而且容易RE: 1 #include <iostream> 2 #include <cmath> 3 #include <cstdio> 4 #include &l

[DP]Hdu 5234 Happy birthday

水题代码: #include <iostream> #include <cstring> using namespace std; const int MAXN=105; int w[MAXN][MAXN]; int dp[MAXN][MAXN][MAXN]; int n,m,kk; int max(int a,int b){ if(a<b)return b; return a; } int main(){ while(cin>>n>>m>>

hdu 5234 (bc #42 C)Happy birthday(dp)

Happy birthday Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 672    Accepted Submission(s): 302 Problem Description Today is Gorwin’s birthday. So her mother want to realize her a wish. Gorwin

hdu 5234 happy birthday (bc #42 C) (DP)

Happy birthday Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 672    Accepted Submission(s): 302 Problem Description Today is Gorwin’s birthday. So her mother want to realize her a wish. Gorwin

hdu 5234

题意:求在不超过k的情况下,最多可以得到多少价值. 三维dp,结合01背包,第三维就是用来保存在不同的背包容量下能得到的最大价值,也就是第三维有很多状态. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include<queue> #include<st