原题: http://acm.hdu.edu.cn/showproblem.php?pid=5234

题目:

Happy birthday

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 686 Accepted Submission(s): 312

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.

思路:

从左上角出发,走到右下角,每次可以向右或者向下走,走过的地方有蛋糕数量,求不超过指定数量的最大值。

如果只能向右走或者向下走,那么这是个01背包。
现在可以向右和向下,其实也是个01背包。

三维数组dp[i][j][k],其中ij分别表示坐标,k表示当前当前背包容量,dp值为真表示满足从原点到ij装k个蛋糕的情况,dp值为假则表示不满足。
状态转移方程:dp[i][j][k]=max(max(dp[i-1][j][k],dp[i-1][j][k-a[i][j]]+a[i][j]),max(dp[i][j-1][k],dp[i][j-1][k-a[i][j]]+a[i][j]))
和普通的转移方程相比,它只是比较了两个方向而已。

代码:

//#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=105;
int a[N][N];        //重量
int dp[N][N][N];    //加入到xy位置背包容量为z情况下的总重量,即为记录的最优解
void init()
{memset(dp,0,sizeof(dp));
}
void gao(int n,int m,int v)
{for(int i=a[0][0]; i<=v; ++i){dp[0][0][i]=a[0][0];}for(int i=1; i<n; ++i)  //从头向下遍历{for(int j=0; j<=v; ++j) //j当前容量{if(j<a[i][0])       //装不下当前物品dp[i][0][j]=dp[i-1][0][j];  //最优解即为不装本次的情况,即上次的情况elsedp[i][0][j]=max(dp[i-1][0][j],dp[i-1][0][j-a[i][0]]+a[i][0]);   //装得下,上次最优解和放入的最优解}}for(int i=1; i<m; ++i)  //从头向右遍历{for(int j=0; j<=v; ++j){if(j<a[0][i])dp[0][i][j]=dp[0][i-1][j];elsedp[0][i][j]=max(dp[0][i-1][j],dp[0][i-1][j-a[0][i]]+a[0][i]);}}for(int i=1; i<n; ++i)  //向右下双向打表{for(int j=1; j<m; ++j){for(int k=0; k<=v; ++k){if(k<a[i][j])dp[i][j][k]=max(dp[i-1][j][k],dp[i][j-1][k]);elsedp[i][j][k]=max(max(dp[i-1][j][k],dp[i-1][j][k-a[i][j]]+a[i][j]),max(dp[i][j-1][k],dp[i][j-1][k-a[i][j]]+a[i][j]));}}}printf("%d\n",dp[n-1][m-1][v]);
}
int main()
{int n,m,k;while(scanf("%d%d%d",&n,&m,&k)!=EOF){init();for(int i=0; i<n; ++i)for(int j=0; j<m; ++j)scanf("%d",&a[i][j]);gao(n,m,k);}return 0;
}

HDU 5234 Happy birthday 最多蛋糕相关推荐

  1. hdu 5234 动态规划

    http://acm.hdu.edu.cn/showproblem.php?pid=5234 1 1 2 //n,m,k(1--100) 3 2 3 100 1 2 3 4 5 6 Sample Ou ...

  2. hdu 5234 Happy birthday

    hdu 5234 Happy birthday 题意: 今天是Gorwin的生日.所以她的妈妈要实现她的一个愿望.Gorwin说她想吃很多蛋糕.所以他妈妈带她来到了蛋糕园. 这个园子被分成了n*m个方 ...

  3. hdu 5234 Happy birthday 背包 dp

    Happy birthday Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  4. HDU 5234 Happy birthday 01背包

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5234 bc:http://bestcoder.hdu.edu.cn/contests/con ...

  5. hdu 5234 - Happy birthday DP

    Happy birthday Accepts: 178 Submissions: 511 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...

  6. HDU 5234 DP背包

    题意:给一个n*m的矩阵,每个点是一个蛋糕的的重量,然后小明只能向右,向下走,求在不超过K千克的情况下,小明最终能吃得最大重量的蛋糕. 思路:类似背包DP: 状态转移方程:dp[i][j][k]--- ...

  7. POJ3122贪心或者二分(分蛋糕)

    题意:        m+1个人来分n个蛋糕,每个人分到的蛋糕数必须一样而且还必须是同一个蛋糕上的,问每个人最多分多少蛋糕? 思路:      能想到的方法有两种,一个是直接贪心,另一个就是二分,这个 ...

  8. vscode写php高亮,写了一个Hy的vscode语法高亮插件

    -------2018 8 3----------- 把函数名和参数改了,正则有点古怪,参考自带的lambda表达式才搞定 但彩色括号走了弯路,各种配图有彩色括号的插件其实很少是自己搞的,其实只要再装 ...

  9. 如何查看linux网络io,linux 查看CPU内存 网络 流量 磁盘 IO

    使用vmstat命令来察看系统资源情况 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? Q: 在命令行方式下,如何查看CPU.内存的使用情况,网络流量和磁盘I/O? A: 在命 ...

最新文章

  1. python学习笔记之编写readConfig读写配置文件
  2. 【stanford C++】——2.C++中函数
  3. html div实时监听,jquery实时监听div高度变化
  4. MySQL学习笔记02【SQL基本概念与通用语法、数据库的CRUD操作】
  5. OpenCV Image Pyramids影像金字塔
  6. datasg中数据的存储结构
  7. 幼儿课外活动游戏_泰国清迈大小学校介绍 --【Little Star小星星幼儿园】
  8. c/c++这么难学,那么学会了究竟有多牛X呢?
  9. JavaScript get set方法 ES5/ES6写法
  10. 【笔记】双线性池化(Bilinear Pooling)详解、改进及应用
  11. 怎么看软件的编写代码
  12. php的redis安装配置,Redis 的安装配置介绍_php
  13. Flutter关于简单的吸顶通讯录制作
  14. NSSM部署Net Core流程
  15. 张朝阳5G寻路与搜狐奇兵
  16. 如何解决u盘不能拷贝超过4G的大文件
  17. Web语音播报提示音
  18. tensorflow 2.0 Layer定义的源码分析
  19. 全国计算机专业录取分数线排名,2020中国高校录取分数线排名全国大学录取分数线表...
  20. 微计算机断层扫描的用途以及测试样品要求

热门文章

  1. html设计一个网页表格,21个新奇古怪的网页表格设计
  2. 将Planet卫星影像数据添加到QGIS, ArcGIS Pro 或 ArcGIS 10.X方法,以ArcGIS Pro为例。
  3. 电脑变卡DOS解决方案
  4. word 如何删除边框黑线
  5. Red Hat Ubuntu Centos 更改登录横幅
  6. 行人轨迹论文:STUGCN:A Social Spatio-Temporal Unifying Graph Convolutional Network for Trajectory Predictio
  7. revit图纸导出dxf文件批量修改
  8. DDD之实体与值对象区别
  9. POSCMS 域名绑定
  10. 数学建模快速入门(6)二维图和三维图