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;
     }
  • 相关阅读:
    c语言 作用域、存储期、链接属性汇总
    进程上下文切换分析
    进程装载过程分析(execve系统调用分析)
    fork 创建进程的过程分析
    系统调用软中断处理程序system_call分析
    linux 系统调用分析
    8分钟带你深入浅出搞懂Nginx
    控制反转
    JAVA泛型(转)
    AOP(转)
  • 原文地址:https://www.cnblogs.com/albert67/p/10314836.html
Copyright © 2011-2022 走看看