(hdu)5234 Happy birthday 二维dp+01背包
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5234
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(,) 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+,j) or (i,j+) (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 ), 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. <=n,m,K<= <=wij<= Output
For each case, output an integer in an single line indicates the maximum weight of cake Gorwin can eat. Sample Input Sample Output 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 (,)->(,)->(,)->(,). When she passes a grid, she eats up the cake in that grid. Thus the total amount cake she eats is +++=.
题意:一个N*M的方阵,每个值代表蛋糕量,只能选择吃完或不吃,从(1,1)只能向下和向右走的情况下到(n,m)。问在不超过K值情况下,最多能吃多少?
方法:用dp[i][j][k]表示在(i,j)点不超过k的情况下能吃多少
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
#define met(a,b) memset(a,b,sizeof(a));
const int oo = 0x3f3f3f3f;
const int N = ;
int a[N][N],dp[N][N][N];
int main()
{
int n,m,k;
while(scanf("%d %d %d",&n,&m,&k)!=EOF)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
scanf("%d",&a[i][j]);
}
met(dp,);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
for(int l=k;l>=a[i][j];l--)
{
int x=max(dp[i-][j][l],dp[i][j-][l]);///表示i,j这个点如果不拿能拿到的最大值
int y=max(dp[i-][j][l-a[i][j]]+a[i][j],dp[i][j-][l-a[i][j]]+a[i][j]);
///如果在不超过k的情况下拿i,j这个点所能拿的最大值
dp[i][j][l]=max(x,y);
}
}
}
printf("%d\n",dp[n][m][k]);
}
return ;
}
(hdu)5234 Happy birthday 二维dp+01背包的更多相关文章
- HDU - 2159 FATE(二维dp之01背包问题)
题目: 思路: 二维dp,完全背包,状态转移方程dp[i][z] = max(dp[i][z], dp[i-1][z-a[j]]+b[j]),dp[i][z]表示在杀i个怪,消耗z个容忍度的情况下 ...
- HDU 2923 Relocation(状压dp+01背包)
题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...
- 洛谷P1048 采药 二维dp化一维
题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个 ...
- HDU 2159 FATE【二维完全背包】
题意:xhd玩游戏,还需要n个经验值升级,还留有m的忍耐度,但是他最多打s只怪,给出k个怪的经验值a[i],以及消耗的忍耐度b[i],问xhd能不能升级-- 因为有两个限定,忍耐度,和最多打s只怪(即 ...
- 洛谷p1732 活蹦乱跳的香穗子 二维DP
今天不BB了,直接帖原题吧 地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- 传纸条 NOIP2008 洛谷1006 二维dp
二维dp 扯淡 一道比较基本的入门难度的二维dp,类似于那道方格取数,不过走过一次的点下次不能再走(看提交记录里面好像走过一次的加一次a[i][j]的也AC了,,),我记得当年那道方格取数死活听不懂, ...
随机推荐
- linux统计文件夹某一些文件的大小总和
du -m smallgame_2006* | awk '{sum += $1}; END{print sum}' -m代表单位是MB, awk命令需要'',且命令需换行
- HW5.11
public class Solution { public static void main(String[] args) { System.out.printf("%s\t%s\n&qu ...
- Task任务
- PC-老鸟装机
老鸟装机 一.硬件安装莫疏忽 1>安装硬盘有讲究 1.单硬盘+单光驱 IDE1----硬盘(Mastet接口) IDE2----光驱(Mastet接口) ...
- BOM 和 JavaScript 中的 trim
今天遇到一个 IE7 下 JSON.parse 失败的问题.经过排查发现:服务端某个配置文件编码是 UTF-8 + BOM,输出的字符串最开始包含了 BOM 字符,不是合法的 JSON. IE7 不支 ...
- hdoj 1495 非常可乐【bfs隐式图】
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- map的基本操作函数及含义
map的基本操作函数: C++ Maps是一种关联式容器,包含“关键字/值”对 begin() 返回指向map头部的迭代器 clear() ...
- Spring Framework 5.0.0.M3中文文档 翻译记录 introduction
翻译自: http://docs.spring.io/spring/docs/5.0.0.M3/spring-framework-reference/htmlsingle/#spring.tld.ha ...
- jfinal获取当前访问路径和端口号
public void generateSingleLicense() throws Exception { System.out.println(getRequest().getRequestURL ...
- java.sql.SQLException: Lock wait timeout exceeded --转
org.springframework.dao.CannotAcquireLockException 的解决> 直接上 bug 的详细信息: 2012-03-12 15:20:31 XmlBea ...