zoukankan      html  css  js  c++  java
  • HDU 5234

    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{ m{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.
    #include <cstring>
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    #include <cmath>
    using namespace std;
    #define N 112
    int a[200][200];
    int dp[N][N][N];
    int main()
    {
        int n,m,kk;
    
    
        while(scanf("%d%d%d", &n,&m,&kk) != EOF)
        {
            memset(dp,0,sizeof(dp));
            memset(a,0,sizeof(a));
    
            for(int i=1; i<=n; i++)
                for(int j=1; j<=m; j++)
                    scanf("%d",&a[i][j]);
    
            for(int i=1; i<=n; i++)
                for(int j=1; j<=m; j++)
                    for(int k=0; k<=kk; k++)
                    {
                        if(k < a[i][j])
                            dp[i][j][k] = max(dp[i-1][j][k],dp[i][j-1][k]);///当k小于a[i][j]时不拿
                        else
                        {
                            int x = max(dp[i-1][j][k-a[i][j]],dp[i][j-1][k-a[i][j]])+a[i][j];
                            int y = max(dp[i-1][j][k],dp[i][j-1][k]);
                            dp[i][j][k] = max(x,y);
                        }
                    }
    
            printf("%d
    ",dp[n][m][kk]);
    
        }
        return 0;
    }///三维数组,因为走得时候是二维的再加上k是三维
  • 相关阅读:
    USACO Broken Necklace 通过了
    USACO Broken Necklace
    推荐顺序ACM
    usaco暂时无法访问
    格式
    稳定排序
    归并排序
    浅析Struts1和Struts2的Action线程安全问题
    判别式模型与生成式模型的区别
    远景能源一面
  • 原文地址:https://www.cnblogs.com/biu-biu-biu-/p/5778975.html
Copyright © 2011-2022 走看看