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

    Cow Bowling


    Description
    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

    题目大意:

        输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线。

        规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个数中的一个。

    解题思路:

        动态规划。

        dp[i][j]=max(dp[i-1][j],dp[i-1][j-1]+data[i][j]

        因为dp数组的值只与上一行有关,用滚动数组优化了一下。(直接开成一维的了。。。)

    Code(Mem:1184K Tim:32MS):

    /*************************************************************************
        > File Name: poj3176.cpp
        > Author: Enumz
        > Mail: 369372123@qq.com 
        > Created Time: 2014年10月21日 星期二 19时38分18秒
     ************************************************************************/
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<string>
    #include<cstring>
    #include<list>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #include<algorithm>
    #define MAXN 351
    using namespace std;
    int way[MAXN][MAXN],dp[MAXN][MAXN];
    int N;
    void Input()
    {
        cin>>N;
        for (int i=1;i<=N;i++)
            for (int j=1;j<=i;j++)
                scanf("%d",&way[i][j]);
    }
    void Solve()
    {
        memset(dp,0,sizeof(dp));
        for (int i=1;i<=N;i++)
            for (int j=1;j<=i;j++)
                dp[i][j]=max(dp[i-1][j],dp[i-1][j-1])+way[i][j];
    }
    void Output()
    {
        int ret=dp[N][1];
        for (int i=2;i<=N;i++)
            if (ret<dp[N][i]) ret=dp[N][i];
        cout<<ret<<endl;
    }
    int main()
    {
        Input();
        Solve();
        Output();
        return 0;
    }

    Code(滚动数组优化 Mem:224K Tim:47MS):

     1 /*************************************************************************
     2     > File Name: poj3176.cpp
     3     > Author: Enumz
     4     > Mail: 369372123@qq.com
     5     > Created Time: 2014年10月21日 星期二 19时38分18秒
     6  ************************************************************************/
     7 #include<iostream>
     8 #include<cstdio>
     9 #include<cstdlib>
    10 #include<string>
    11 #include<cstring>
    12 #include<list>
    13 #include<queue>
    14 #include<stack>
    15 #include<map>
    16 #include<set>
    17 #include<algorithm>
    18 #define MAXN 351
    19 using namespace std;
    20 int way[MAXN],dp[MAXN];
    21 int N;
    22 void Input()
    23 {
    24     cin>>N;
    25     memset(dp,0,sizeof(dp));
    26     for (int i=1; i<=N; i++)
    27     {
    28         for (int j=1; j<=i; j++)
    29             scanf("%d",&way[j]);
    30         for (int j=i; j>=1; j--)
    31             dp[j]=max(dp[j],dp[j-1])+way[j];
    32     }
    33 }
    34 void Output()
    35 {
    36     int ret=dp[1];
    37     for (int i=2; i<=N; i++)
    38         if (ret<dp[i]) ret=dp[i];
    39     cout<<ret<<endl;
    40 }
    41 int main()
    42 {
    43     Input();
    44     Output();
    45     return 0;
    46 }
  • 相关阅读:
    杨晓峰-Java核心技术-6 动态代理 反射 MD
    ARouter 路由 组件 跳转 MD
    领扣-5 最长回文子串 Longest Palindromic Substring MD
    算法 递归 迭代 动态规划 斐波那契数列 MD
    二叉树 遍历 先序 中序 后序 深度 广度 MD
    算法 数组中出现次数最多的数字 MD
    领扣-754 到达终点数字 Reach a Number MD
    领扣-1/167 两数之和 Two Sum MD
    文件 File 常见操作 工具 MD
    IO流 简介 总结 API 案例 MD
  • 原文地址:https://www.cnblogs.com/Enumz/p/4094704.html
Copyright © 2011-2022 走看看