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.
Output
For each case, output an integer in an single line indicates the maximum weight of cake Gorwin can eat.
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.
Mean:
给一个n*m的矩阵,每个点是一个蛋糕的的重量,
然后Gorwin只能向右,向下走,求在不超过K千克的情况下,Gorwin最终能吃得最大重量的蛋糕.
anlayse:
类似背包DP;
状态转移方程:dp[i][j][k]----在i,j位置时,最大容量为k时的最大值;
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=109;
const int INF=0x3f3f3f3f;
const int mod=2009;
int dp[maxn][maxn][maxn];
int num[maxn][maxn];
int main()
{
int n, m, k;
while(~scanf("%d %d %d", &n, &m, &k))
{
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
scanf("%d", &num[i][j]);
memset(dp, 0, sizeof(dp));
for(int i=1; i<=n; i++)
{
for(int 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;
}