• 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;
    }


  • 相关阅读:
    全方位讲解 Nebula Graph 索引原理和使用
    一首古诗带来的图数据库大冒险
    Nebula Graph 在众安保险的图实践
    springioc源码
    leetcode原地删除系列283移除0
    leetcode21单链表问题
    20天备考初级会计
    leetcode原地删除系列27
    leetcode 在排序数组中查找元素的第一个和最后一个位置
    内置后置处理器要理解的问题
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203981.html
Copyright © 2020-2023  润新知