zoukankan      html  css  js  c++  java
  • hdu 5234 (bc #42 C)Happy birthday(dp)

    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 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 wij 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 wi1,wi2,wi3,wim 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<=wij<=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.
     
    Source
     
    Recommend
    hujie   |   We have carefully selected several similar problems for you:  5338 5337 5336 5335 5334 
     
    01 背包,二维而已
    一遍ac,真是好开心
    01背包对于第i个物品的状态是只能由 i-1得到 (所以倒着循环,在计算i-1状态的时候总保证i的状态已经写出来了,可以省一维空间)
    而这道题在于,对于(i,j) 的状态,可以由 (i-1,j) 和 (i,j-1)得到,每一个对应取或者不取,一共就是四种状态
    即:由上面不取当前得到,由上面取当前得到,由左边不取当前得到,由左边取当前得到。
    状态转移方程为:
    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);
    初始化很容易想到,没取的时候价值是0
    最后答案在 dp[n][m][i]  (1=<i<=k )取最大的....
     1 /*************************************************************************
     2     > File Name: code/bc/#42/C.cpp
     3     > Author: 111qqz
     4     > Email: rkz2013@126.com 
     5     > Created Time: 2015年08月01日 星期六 10时52分24秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<iomanip>
    10 #include<cstdio>
    11 #include<algorithm>
    12 #include<cmath>
    13 #include<cstring>
    14 #include<string>
    15 #include<map>
    16 #include<set>
    17 #include<queue>
    18 #include<vector>
    19 #include<stack>
    20 #define y0 abc111qqz
    21 #define y1 hust111qqz
    22 #define yn hez111qqz
    23 #define j1 cute111qqz
    24 #define tm crazy111qqz
    25 #define lr dying111qqz
    26 using namespace std;
    27 #define REP(i, n) for (int i=0;i<int(n);++i)  
    28 typedef long long LL;
    29 typedef unsigned long long ULL;
    30 const int inf = 0x7fffffff;
    31 const int N=1E2+5;
    32 int a[N][N];
    33 int n,m,k;
    34 int dp[N][N][N];
    35 
    36 int mx(int x1,int x2,int x3,int x4)
    37 {
    38     int  res = -1;
    39     if (x1>res) res = x1;
    40     if (x2>res) res = x2;
    41     if (x3>res) res = x3;
    42     if (x4>res) res = x4;
    43     return res;
    44 }
    45 void solve (int x,int y,int cost, int value)
    46 {
    47     for ( int v = k ; v >= cost ; v--)
    48     {
    49     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);
    50     }
    51 }
    52 int main()
    53 {
    54     while (scanf("%d %d %d",&n,&m,&k)!=EOF)
    55     {
    56     for ( int i = 1 ; i <= n ; i++ )
    57     {
    58         for ( int j = 1;  j <= m ; j++ )
    59         {
    60         scanf("%d",&a[i][j]);
    61         }
    62     }
    63     memset(dp,0,sizeof(dp));
    64     for ( int i = 1 ; i <= n ; i++ )
    65     {
    66         for ( int j = 1 ; j <= m ; j++ )
    67         {
    68         solve(i,j,a[i][j],a[i][j]);
    69         }
    70     }
    71     int ans = -1;
    72     for ( int i = 1 ; i <= k ; i++)
    73     {
    74         ans = max (ans,dp[n][m][i]);
    75     }
    76     cout<<ans<<endl;
    77     }
    78   
    79     return 0;
    80 }
  • 相关阅读:
    抓取csdn上的各类别的文章 (制作csdn app 二)
    数塔 动态规划
    Android 使用Fragment,ViewPagerIndicator 制作csdn app主要框架
    钥匙计数之二
    FusionCharts简单教程(三)-----如何自定义图表上的工具提示
    位图像素的颜色 携程编程大赛hdu
    Android 仿Win8的metro的UI界面(上)
    Android 省市县 三级联动(android-wheel的使用)
    JDK源码学习系列01----String
    互联网投稿渠道
  • 原文地址:https://www.cnblogs.com/111qqz/p/4693854.html
Copyright © 2011-2022 走看看