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;
}

最新文章

  1. 手动安装Oracle的Maven依赖
  2. Win10 UI动画
  3. 2013 duilib入门简明教程 -- 复杂控件介绍 (13)
  4. 我心中的核心组件(可插拔的AOP)~第四回 异常拦截器
  5. 字节流和字符流(BufferedReader类和BufferedWriter类)
  6. sql语句练习50题
  7. C#记录对象的变化
  8. 使用 IL 实现类型转换
  9. Redis 在windows环境下安装
  10. ASP.NET使用Jquery-Ajax向ashx传递参数中文出现乱码
  11. maven install 报错Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin
  12. js基础例子dom+原型+oop基础知识记录01
  13. hihoCoder 1388 Periodic Signal(FFT)
  14. UDP 通信
  15. VR的发展历程-VR全景智慧城市
  16. xml解析案例
  17. 腾讯云服务器php+mysq+nginx配置出现的问题及解决方法(亲测)
  18. APP性能测试(CPU)
  19. nodeJS基于smtp发邮件
  20. setInterval()使用时易疏忽的点

热门文章

  1. 【C_Language】---一份程序看懂C程序printf()的几种常用用法
  2. poj 2689 区间素数筛
  3. Pycharm 中的翻译工具
  4. 关于后缀间$LCP$的一些公式的证明
  5. java 赋值运算
  6. 基于selenium爬取京东
  7. 多个github账号时,本地配置ssh-key
  8. Ubuntu下cc和gcc的关系
  9. python3操作MySQL的模块pymysql
  10. SpringBoot缓存篇Ⅰ--- 缓存抽象