Happy Birthday
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 672 Accepted Submission (s): 302
Problem Descriptiontoday is Gorwin ' s birthday. So she mother want to realize her a wish. Gorwin says that she wants to eat many cakes. Thus, her mother takes she to a cake garden.
The garden was splited into n*m grids. In each grids, there is a cake. The weight of cake in the i-th row j-th column isWiJ Kilos, Gorwin starts from the Top-left (all) 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) (Howev Er, she can not be 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 the most K kilos cake. Now, Gorwin have stood in the Top-left grid and look at the "Map of the garden" she want to find a route which can leads her To eat most cake. But the map was so complicated. So she's wants you.
Inputmultiple test Cases, every case gives N, M, K
In the next n lines, the i-th line contains m integersWI1,WI2 , wi3< Span id= "mathjax-span-43" class= "Mo" >,? w i m Which describes the weight of cakes in the i-th row
Please process to the end of file.
[Technical specification]
All inputs is integers.
1<=n,m,k<=100
1<=WiJ<=100
Outputfor each case, output a integer in a single line indicates the maximum weight of cake gorwin can eat.
Sample INPUT1 1 232 3 1001 2 34 5 6
Sample Output016
HintIn 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 (+ 2,1)--(2,2). 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.
Sourcebestcoder Round #42
Recommendhujie | We have carefully selected several similar problems for you:5338 5337 5336 5335 5334 01 backpack, two-dimensional just once AC, it's good to be happy. 01 Backpack for the status of the first item Can only be obtained by i-1 (so backwards loop, in the calculation of the i-1 state of the total guarantee I state has been written out, you can save one dimensional space) and this problem is, for (I,J) the state, can be (i-1,j) and (i,j-1) get, each corresponding to take or not take, Altogether is four states namely: by the above does not fetch the current get, which is obtained by the above current, which is obtained by the left not at present, which is obtained by the left fetch at present. The state transition equation is:
DP[X][Y][V]=MX (dp[x-1][y][v],dp[x][y-1][v],dp[x-1][y][v-cost]+value,dp[x][y-1][v-cost]+value);
Initialization is very easy to think, when the value is not taken is 0 the final answer in Dp[n][m][i] (1=<i<=k) take the largest ....
1 /*************************************************************************2 > File name:code/bc/#42/c.cpp3 > Author:111qqz4 > Email: [Email protected]5 > Created time:2015 August 01 Saturday 10:52 24 seconds6 ************************************************************************/7 8#include <iostream>9#include <iomanip>Ten#include <cstdio> One#include <algorithm> A#include <cmath> -#include <cstring> -#include <string> the#include <map> -#include <Set> -#include <queue> -#include <vector> +#include <stack> - #defineY0 ABC111QQZ + #defineY1 HUST111QQZ A #defineYn hez111qqz at #defineJ1 CUTE111QQZ - #defineTM CRAZY111QQZ - #defineLR DYING111QQZ - using namespacestd; - #defineREP (i, n) for (int i=0;i<int (n); ++i) -typedefLong LongLL; intypedef unsignedLong LongULL; - Const intINF =0x7fffffff; to Const intn=1e2+5; + intA[n][n]; - intn,m,k; the intDp[n][n][n]; * $ intmxintX1,intX2,intX3,intx4)Panax Notoginseng { - intres =-1; the if(x1>res) res =X1; + if(x2>res) res =x2; A if(x3>res) res =X3; the if(x4>res) res =x4; + returnRes; - } $ voidSolve (intXintYintCostintvalue) $ { - for(intv = k; V >= cost; v--) - { theDP[X][Y][V]=MX (dp[x-1][y][v],dp[x][y-1][v],dp[x-1][y][v-cost]+value,dp[x][y-1][v-cost]+value); - }Wuyi } the intMain () - { Wu while(SCANF (" %d%d%d", &n,&m,&k)! =EOF) - { About for(inti =1; I <= N; i++ ) $ { - for(intj =1; J <= M; J + + ) - { -scanf"%d",&a[i][j]); A } + } theMemset (DP,0,sizeof(DP)); - for(inti =1; I <= N; i++ ) $ { the for(intj =1; J <= M; J + + ) the { the solve (i,j,a[i][j],a[i][j]); the } - } in intAns =-1; the for(inti =1; I <= K; i++) the { AboutAns =Max (ans,dp[n][m][i]); the } thecout<<ans<<Endl; the } + - return 0; the}
Hdu 5234 (BC #42 C) Happy Birthday (DP)