(HDU) 5234 Happy Birthday Two-dimensional dp+01 backpack

Source: Internet
Author: User

Title Link: http://acm.split.hdu.edu.cn/showproblem.php?pid=5234

problem Descriptiontoday isGorwin ' 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 isSplited into n*m grids. In each grids, there isA cake. The weight of cakeinchThe i-th row j-th column isWij kilos, Gorwin starts fromThe 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 standsinch(i,j), then she can go to (i+1, j) or (i,j+1) (However, she can not go outof the garden ). When Gorwin reachs a grid, she can eat up the cakeinchThat grid or just leave it alone. However she can ' t eat part of the cake. But Gorwin ' s belly isNot very large, so she can eat at the most K kilos cake. Now, Gorwin have stoodinchThe 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 isSo complicated. So she's wants you. Inputmultiple test Cases ( about the), every CaseGives N, M, Kincha single line. In the next n lines, the I-th line contains m integers wi1,wi2,wi3,?wim which describes the weight of cakesinchThe I-th rowplease process to the end of file. [Technical specification] All inputs is integers.1<=n,m,k<= -1<=wij<= -outputfor each Case, Output an integerinchA single line indicates the maximum weight of cake gorwin can eat. Sample Input1 1 232 3  -1 2 34 5 6Sample Output0 -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 (1,1), (2,1), (2,2), (2,3). When she passes a grid, she eats up the cakeinchThat grid. Thus The total amount cake she eats is 1+4+5+6= -.

Test instructions: A n*m square, each value represents the amount of cake, can only choose to eat or not eat, from (a) can only go down and to the right of the case to (N,M). Q How much can I eat without exceeding the K value?

Method: Using dp[i][j][k] to indicate at (I,J) point not more than k can eat how much

#include <cstdio>#include<cstring>#include<queue>#include<cstdlib>#include<algorithm>#include<iostream>using namespacestd;#definell Long Long#defineMet (A, b) memset (A,b,sizeof (a));Const intOO =0x3f3f3f3f;Const intN =107;intA[n][n],dp[n][n][n];intMain () {intn,m,k;  while(SCANF (" %d%d%d", &n,&m,&k)! =EOF) {         for(intI=1; i<=n;i++)        {             for(intj=1; j<=m;j++) scanf ("%d",&A[i][j]); } Met (DP,0);  for(intI=1; i<=n;i++)        {             for(intj=1; j<=m;j++)            {                 for(intl=k;l>=a[i][j];l--)                {                    intX=max (dp[i-1][j][l],dp[i][j-1][l]);///I,j This point if you don't get the maximum value you can get.                    intY=max (dp[i-1][j][l-a[i][j]]+a[i][j],dp[i][j-1][l-a[i][j]]+A[i][j]); ///If you take i,j without exceeding K, the maximum value that this point can takedp[i][j][l]=Max (x, y); }}} printf ("%d\n", Dp[n][m][k]); }    return 0;}

(HDU) 5234 Happy Birthday Two-dimensional dp+01 backpack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.