zoukankan      html  css  js  c++  java
  • poj3176(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

    最基础的数塔dp了,维数可以降到一维来优化空间,不过这样要注意循环顺序,以免覆盖之前结果

    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <set>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <algorithm>
    #include <functional>
    #include <iomanip>
    #include <limits>
    #include <new>
    #include <utility>
    #include <iterator>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    using namespace std;
    typedef long long LL;
    const int INF = 0x3f3f3f3f;
    const double PI = acos(-1.0);
    const double EPS = 1e-8;
    const int MAXN = 360;
    int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
    
    int main()
    {
        int n;
        int s[MAXN][MAXN], dp[MAXN];
        cin >> n;
        for (int i = 0; i < n; ++i)
            for (int j = 0; j <= i; ++j)
                scanf("%d", &s[i][j]);
        for (int i = 0; i < n; ++i)
            dp[i] = s[n-1][i];
        for (int i = n-2; i >= 0; --i)
            for (int j = 0; j <= i; ++j)
                dp[j] = max(dp[j], dp[j+1]) + s[i][j];
        cout << dp[0] << endl;
        return 0;
    }


  • 相关阅读:
    GTK+中的树状列表构件(GtkTreeView)
    [TOP]疯狂的投资
    多线程模式之MasterWorker模式
    一年读书总结
    Microsoft Visual Studio正忙解决办法
    使用vs自带的性能诊断工具
    C#中的扩展方法
    从委托、匿名方法到Lambda
    c#中的事件
    sqlserver中创建包含事务的存储过程
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203981.html
Copyright © 2011-2022 走看看