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;
     }
  • 相关阅读:
    VS2010 修改模板文件,增加默认注释
    Substring方法(C#,JS,Java,SQL)的区别
    好的架构是进化来的,不是设计来的
    SQL Server编程系列(2):SMO常用对象的有关操作
    (转) SQL Server编程系列(1):SMO介绍
    SQL Server的分页优化及Row_Number()分页存在的问题
    CTE(Common Table Expression) 公用表表达式
    SSH加密原理、RSA非对称加密算法学习与理解
    asp.net 项目目录说明
    【转】SOA架构设计经验分享—架构、职责、数据一致性
  • 原文地址:https://www.cnblogs.com/albert67/p/10314836.html
Copyright © 2011-2022 走看看