hdu---5234
来源:互联网 发布:教师网络培训总结 编辑:程序博客网 时间:2024/06/05 20:54
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.
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
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 232 3 1001 2 34 5 6
Sample Output
016Mean:给一个n*m的矩阵,每个点是一个蛋糕的的重量,然后Gorwin只能向右,向下走,求在不超过K千克的情况下,Gorwin最终能吃得最大重量的蛋糕.
anlayse:类似背包DP;状态转移方程:dp[i][j][k]----在i,j位置时,最大容量为k时的最大值;
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. #include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<iostream>#include<algorithm>using namespace std;#define N 110int dp[N][N][N];int num[N][N];int main(){ int i, j, n, m, k; while(~scanf("%d%d%d", &n,&m,&k)) { for(i=1; i<=n; i++) { for(j=1;j<=m;j++) { scanf("%d",&num[i][j]); } } memset(dp, 0, sizeof(dp)); for(i=1;i<=n;i++) { for(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;}
0 0
- hdu---5234
- hdu 5234 动态规划
- hdu 5234 Happy birthday
- hdu-5234(dp)
- hdu 5234 Happy birthday
- hdu 5234-三维背包
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- EditText 动态获取输入字数,限制长度
- Vim 开发环境配置
- 秒杀多线程第八篇 经典线程同步 信号量Semaphore
- POJ - 3735 Training little cats(矩阵快速幂)
- Hive 分区,静态分区,动态分区
- hdu---5234
- 关于Spring定时任务(定时器)用法
- hdu3709 Balanced Number(数位dp)
- equals方法的重写
- 大数系列——大数除法以及求模
- POJ 3261 Milk Patterns , Hash
- 软件工程——需求分析
- ScrollingImageView 横向滚动的image view
- Linux-CentOS XAMPP Apache 启用GZIP压缩功能 mod_deflate的安装配置