• 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.

    大致题意:给你一个如上所示的数字金字塔,从第一行开始往下加,每行只能选取一个数字,下一个选取的数字只能位于上一个选取数字的对角线位置上,问最大和为多少

    问题分析:我们由下往上取,由于上面的一行的宽度总是小于下面的一行,所以我们到着dp,保证最优子结构,当遍历回第一行是,dp[0][0]即使最优解。

    in:

    5
    7
    3 8
    8 1 0
    2 7 4 4
    4 5 2 6 5

    out:

    30

    状态转移方程:a[i][j]+=max(a[i+1][j+1],a[i+1][j]);

    #include<iostream>
    using namespace std;
    int n;
    int a[350][350];
    int total;
    int main()
    {
        cin>>n;
            for(int i=0;i<n;i++)
                for(int j=0;j<=i;j++)
                cin>>a[i][j];
            for(int i=n-2;i>=0;i--)
                for(int j=0;j<=i;j++)
                a[i][j]+=max(a[i+1][j+1],a[i+1][j]);
            cout<<a[0][0]<<endl;
        return 0;
    }
  • 相关阅读:
    Javaoop_继承
    事务,视图,索引,备份和恢复
    常用函数。
    为虚拟机配置NAT网络
    安装Linux系统
    初始Redis
    高内聚低耦合
    MyBatis动态SQL
    初始MyBatis
    GitHub的操作
  • 原文地址:https://www.cnblogs.com/iloveysm/p/12261108.html
Copyright © 2020-2023  润新知