poj 3176 Cow Bowling

来源:互联网 发布:软件测试核心期刊 编辑:程序博客网 时间:2024/06/07 23:38
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   5Then 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, NLines 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

573 88 1 02 7 4 44 5 2 6 5

Sample Output

30

Hint

Explanation of the sample:          7         *        3   8       *      8   1   0       *    2   7   4   4       *  4   5   2   6   5The highest score is achievable by traversing the cows as shown above.

【题意】给你一个数字三角形,让你从顶到底找一条路,使路上的数字之和最大。当你选择了某一个数的时候,就只能选择两肩上的数字。

【分析】简单dp,从最后一行开始往上走,每次到了一个节点的值一定是当前值加上两条腿上的最大值,依次这样处理到顶就可以得到最大值。转移方程:

num[i][j]+=max(num[i+1][j],num[i+1][j+1]);

【代码】

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int num[400][400];int main(){    int n;    scanf("%d",&n);    for(int i=0; i<n; i++)        for(int j=0; j<=i; j++)            scanf("%d",&num[i][j]);    for(int i=n-2; i>=0; i--)        for(int j=0; j<=i; j++)            num[i][j]+=max(num[i+1][j],num[i+1][j+1]);    printf("%d\n",num[0][0]);    return 0;}
原创粉丝点击