题目链接: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背包的更多相关文章

  1. 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个容忍度的情况下 ...

  2. HDU 2923 Relocation(状压dp+01背包)

    题目代号:HDU2923 题目链接:http://poj.org/problem?id=2923 Relocation Time Limit: 1000MS Memory Limit: 65536K ...

  3. 洛谷P1048 采药 二维dp化一维

    题目描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:“孩子,这个 ...

  4. HDU 2159 FATE【二维完全背包】

    题意:xhd玩游戏,还需要n个经验值升级,还留有m的忍耐度,但是他最多打s只怪,给出k个怪的经验值a[i],以及消耗的忍耐度b[i],问xhd能不能升级-- 因为有两个限定,忍耐度,和最多打s只怪(即 ...

  5. 洛谷p1732 活蹦乱跳的香穗子 二维DP

    今天不BB了,直接帖原题吧  地址>>https://www.luogu.org/problem/show?pid=1732<< 题目描述 香穗子在田野上调蘑菇!她跳啊跳,发现 ...

  6. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  7. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  8. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

  9. 传纸条 NOIP2008 洛谷1006 二维dp

    二维dp 扯淡 一道比较基本的入门难度的二维dp,类似于那道方格取数,不过走过一次的点下次不能再走(看提交记录里面好像走过一次的加一次a[i][j]的也AC了,,),我记得当年那道方格取数死活听不懂, ...

随机推荐

  1. 关于Eclispse连接Mysql的Jdbc

    1.在Eclipse中新建Java工程 2.引入JDBC库(在bulid path 的extenrnal里) 3. 1)导入sql包(import java.sql.*) 2)加载(注册)mysql ...

  2. 2013 ACM/ICPC Asia Regional Changsha Online–C (模拟)

    题目描述 略... 题解 注意控制精度即可....变量全部定义成double,结果round就行....妈蛋....被这题目恶心死了.... 代码: #include <iostream> ...

  3. ios get airplay name

    tarting from iOS7 AudioToolbox API for currentRoute becomes deprecated: Apple instead made currentRo ...

  4. nyoj 27 水池数目

    水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳理工学院校园里有一些小河和一些湖泊,现在,我们把它们通一看成水池,假设有一张我们学校的某处的地图,这个地 ...

  5. Ubuntu16.04编译安装php

    #Ubuntu16.04编译安装php Ubuntu16.04上面搭建基于Nginx的php服务.Nginx使用apt直接安装的. sudo apt install nginx php的安装部署步骤主 ...

  6. HTML5 中的一些新特性

    HTML5是HTML最新的修订版本,包含了新的标签元素,属性和行为,同时包含了一系列可以被用来让 Web 站点和应用更加多样化,功能更强大的技术.HTML5实现了不依赖flash插件播放视频,而且引入 ...

  7. cmd 里面运行git提示“不是内部或外部命令,也不是可运行的程序”的解决办法

    1.找到你电脑上的git安装中bin的路径,如:E:\安装吧\Git\Git\bin:同时,找到git安装路径中git-core的位置,如:E:\安装吧\Git\Git\libexec\git-cor ...

  8. DataSet、DataTable和DataGridView知识备忘

    datatable中,获取第i行j列的单元格内容:             string str = DataSet.Tables[0].Rows[i][j].ToString():datagridv ...

  9. Override ListView getAdapter造成的后果

    近期工作中,发现了一个bug,是和ListView Adapter有关的.产生了FC,描写叙述信息大约是 "The content of the adapter has changed bu ...

  10. sed命令详解--转

    1.简介 sed是非交互式的编辑器.它不会修改文件,除非使用shell重定向来保存结果.默认情况下,所有的输出行都被打印到屏幕上. sed编辑器逐行处理文件(或输入),并将结果发送到屏幕.具体过程如下 ...