zoukankan      html  css  js  c++  java
  • POJ3176--Cow Bowling(动态规划)

    The cows don't use actual bowling balls when they go bowling. They each take a number (in the range 0..99), though, and line up in a standard bowling-pin-like triangle like this: 

              7
    

    3 8

    8 1 0

    2 7 4 4

    4 5 2 6 5
    Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the way. The cow with the highest score wins that frame. 

    Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.

    Input

    Line 1: A single integer, N 

    Lines 2..N+1: Line i+1 contains i space-separated integers that represent row i of the triangle.

    Output

    Line 1: The largest sum achievable using the traversal rules

    Sample Input

    5
    7
    3 8
    8 1 0
    2 7 4 4
    4 5 2 6 5

    Sample Output

    30


    #include<iostream>
    #include<algorithm>
    #include<string.h>
    using namespace std;
    int data[351][351];
    int dp[351];
    int main(){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=i;j++)
                cin>>data[i][j];
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++){
            for(int j=i;j>=1;j--){//这里为何j要逆序开始算呢?等号右边的dp数组是对应的i-1,如果升学计算的话,等号左边被赋值之后,也就意味着i-1数组被修改了,而下次计算正好需要这次修改的原来的数组的值
                dp[j]=max(dp[j-1],dp[j])+data[i][j];
            }
        }
        int m=dp[1];
        for(int i=2;i<=n;i++){
            if(dp[i]>m)
                m=dp[i];
        }
        cout<<m;
        return 0;
    }
     #include<iostream>
     #include<string.h>
     using namespace std;
     int way[MAXN],dp[MAXN];
     int N;
     void Input()
     {
         cin>>N;
         memset(dp,0,sizeof(dp));
         for (int i=1; i<=N; i++)
         {
             for (int j=1; j<=i; j++)
                 scanf("%d",&way[j]);
             for (int j=i; j>=1; j--)
                 dp[j]=max(dp[j],dp[j-1])+way[j];
         }
     }
     void Output()
     {
         int ret=dp[1];
         for (int i=2; i<=N; i++)
             if (ret<dp[i]) ret=dp[i];
         cout<<ret<<endl;
     }
     int main()
     {
         Input();
         Output();
         return 0;
     }
  • 相关阅读:
    python中的深拷贝和浅拷贝
    Andrew NG 机器学习编程作业6 Octave
    Andrew NG 机器学习编程作业5 Octave
    梯度下降算法对比(批量下降/随机下降/mini-batch)
    无监督算法
    深度学习的方差与偏差
    Andrew NG 机器学习编程作业4 Octave
    数据约束
    数据库的查询
    MySQL的入门与使用,sqlyog对数据库,表和数据的管理
  • 原文地址:https://www.cnblogs.com/albert67/p/10314836.html
Copyright © 2011-2022 走看看