题解 | Cow Pinball-牛客假日团队赛F题

Cow Pinball

https://ac.nowcoder.com/acm/contest/1081/F

题目描述

The cows are playing that cool Japanese 'pinball' game called Pachinko. You've probably seen it, you drop a ball in the top and it hits nails on the way down, veering just a little left or right, until the ball comes out the bottom.
This pachinko game is special: the ball always hits the top nail of the R rows of nails (1 <= R <= 25) and then will hit either the right or left nail underneath that. From there, the ball can hit the next nail that's just a bit lower and to the left or right of the second nail, and so on. The ball never veers too far (i.e., more than 1/2 nail away) from the nail it just hit.
This game also has scoring that's unique: you get points for each little nail you hit on the way down. The cows want to maximize their score on this machine. What is the best score they can achieve?
Here's an example triangle and a good route:

In the sample above, the route from 7 to 3 to 8 to 7 to 5 produces the highest sum: 30. It's not possible to go from 7 to 8 to 8 -- the 8 on the third row is too far away.

输入描述:

* Line 1: A single integer: R
* Lines 2..R+1: Line i+1 contains i space-separated integers that compose the scores of row R of the pachinko machine:

输出描述:

* Line 1: A single integer that is the maximum achievable score.

示例1

输入
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
输出
30
说明
No other achievable sum is higher.

解答

数字三角形DP:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll a[50][50];

int main()
{
    ll maxn=0;
    int r;
    cin>>r;
    for(int i=1;i<=r;i++){
        for(int j=1;j<=i;j++){
            scanf("%lld",&a[i][j]);
        }
    }

    for(int i=r-1;i>=1;i--)//行数
    {
        for(int j=1;j<=i;j++)//列数
        {
           a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
        }
     }
   printf("%lld",a[1][1]);
}


来源:旺 崽
全部评论

相关推荐

09-01 11:31
门头沟学院 Java
buul:七牛云的吧,感觉想法是好的,但是大家没那么多时间弄他这个啊。。。不知道的还以为他是顶尖大厂呢还搞比赛抢hc,只能说应试者的痛苦考察方是无法理解的,他们只会想一出是一出
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务