J - 10

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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.

 
思路:动态转移方程  dp[i][j][l] = max(dp[i][j-1][l], dp[i-1][j][l], dp[i][j-1][l-a[i][j]]+a[i][j], dp[i-1][j][l-a[i][j]]+a[i][j]);
 
代码:
 

#include<stdio.h>
#include<string.h>
#include<math.h>

#define max(a, b)(a > b ? a : b)
#define N 106
int dp[N][N][N];
int a[N][N];

int main()
{
int i, j, n, m, k, l, aa, b, c, d;

while(scanf("%d%d%d", &n, &m, &k) != EOF)
{
memset(dp, 0, sizeof(dp));

for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
scanf("%d", &a[i][j]);

for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
for(l = 0; l <= k; l++)
{
aa = b = c= d;

aa = dp[i][j-1][l];
b = dp[i-1][j][l];

if(l >= a[i][j])//如果物品的体积小于等于当前背包的体积,。
{
c = dp[i][j-1][l-a[i][j]]+a[i][j];//放入后上边物品的价值。
d = dp[i-1][j][l-a[i][j]]+a[i][j];//放入后左边物品的价值。

dp[i][j][l] = max(max(aa, b),max(c, d));

}
else//如果物品的体积大于当前背包的体积, 就不放, 判断左边的点和上边的点那个大。
dp[i][j][l] = max(aa, b);
}
printf("%d\n", dp[n][m][k]);

}
return 0;
}

HDU 5234 背包。的更多相关文章

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

    HDU 5234 题目大意:给定n,m,k,以及n*m(n行m列)个数,k为背包容量,从(1,1)开始只能往下走或往右走,求到达(m,n)时能获得的最大价值 解题思路:dp[i][j][k]表示在位置 ...

  2. HDU 5234 Happy birthday 01背包

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

  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 DP背包

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

  5. HDU 1171 背包

    Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. HDU 1171 Big Event in HDU 多重背包二进制优化

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...

  7. hdu 0-1背包

    题目地址http://acm.hdu.edu.cn/showproblem.php?pid=2602 #include <stdio.h> #include <string.h> ...

  8. hdu 01背包汇总(1171+2546+1864+2955。。。

    1171 题意比较简单,这道题比较特别的地方是01背包中,每个物体有一个价值有一个重量,比较价值最大,重量受限,这道题是价值受限情况下最大,也就值把01背包中的重量也改成价值. //Problem : ...

  9. HUD 1171 Big Event in HDU(01背包)

    Big Event in HDU Problem Description Nowadays, we all know that Computer College is the biggest depa ...

随机推荐

  1. max_element( )

    直接用这个函数 , 会比自己写个for 判断快的多了 . position=max_element(a,a+n)-a; position  代表找到最大元素的位置 , max_element( ) 的 ...

  2. 逆元(inv)

    推荐博客 : http://blog.csdn.net/baidu_35643793/article/details/75268911 通常我们在计算除法取模时,并不能直接的取模后再去相除,答案会有问 ...

  3. 《企业IT架构转型之道:阿里巴巴中台战略思想与架构实战》-总结

      一.什么是业务中台 概念来自于阿里,介于前台和后台(此后台指的是云计算.数据库.消息队列.缓存等基础服务) 采用共享式架构设计解决以往烟囱式架构设计的资源浪费.重复造轮.试错成本高的问题 阿里的中 ...

  4. play framework 相关

    1.下载 官网下载解压,安装有jkd即可使用 2.helloworld $ activator new my-first-app play-java https://www.playframework ...

  5. 关于爬虫的日常复习(7)—— DOM操作及selenium库

  6. Activiti CamelTask(骆驼任务)

    Activiti CamelTask(骆驼任务) 作者:Jesai 人生想讲个不成熟的建议 前言: Camel任务可以从Camel发送和介绍消息,由此强化了activiti的集成功能. 注意camel ...

  7. ORM基础2 字段及其参数和meta

    一.ORM简介 1.概念:ORM(Object Relational Mappingt ),对象关系映射 2.实质:类与数据库之间的映射 3.优点: 开发人员不用写数据库 4.缺点: 开发人员,数据库 ...

  8. 10.JavaSE之包机制

    包机制: 为了更好的组织类,Java提供了包机制,用于区别类名的命名空间 包语句的语法格式为: package pkg1[ . pkg2[ . pkg3...]]; package com.duan. ...

  9. typedef声明变量也是一种求值过程

    前言: 什么叫做:声明变量是求值过程?请看下面的声明, int i; 很简单,声明了个整型变量i,再看如下声明, int *p; 也很简单,立刻反应出来它是指向整型的指针,但是具体如何推倒出来的呢?其 ...

  10. React的环境搭建以及脚手架的安装

    1.安装node.js 如果安装了,就请参照第二步:没有的话,去node.js官网下载:https://nodejs.org/zh-cn/download/ 2.检查 win键+r -----> ...