Cow Bowling POJ

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

Hint

Explanation of the sample: 

          7

*
3 8
*
8 1 0
*
2 7 4 4
*
4 5 2 6 5
The highest score is achievable by traversing the cows as shown above.
 
百度翻译:

牛去打保龄球的时候不使用真正的保龄球。他们每个人都取一个数字(范围在0到99之间),然后像这样排列在一个标准的保龄球柱状三角形中:

          7

3 8

8 1 0

2 7 4 4
 4   5   2   6   5

然后其他奶牛穿过三角形,从它的顶端开始,向下移动到两条对角线相邻的奶牛中的一头,直到到达“底部”行。奶牛的得分是沿途到访的奶牛数量之和。得分最高的牛赢了这一局。给定一个有n(1<=n<=350)行的三角形,确定可实现的最大可能和。

思路:

从三角形的低向上累加,加数值大的,例如在第四行第一个,2与4的和小于2与5的和,因此将这个位置的数更换成7.

 1 #include <cstdio>
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <string>
 9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 1000000007
16 #define eps 1e-6
17 #define ll long long
18 #define INF 0x3f3f3f3f
19 using namespace std;
20 
21 int sz[355][355],n;
22 int main()
23 {
24     scanf("%d",&n);
25     for(int i=0;i<n;i++)
26     {
27         for(int j=0;j<i+1;j++)
28         {
29             scanf("%d",&sz[i][j]);
30         }
31     }
32     for(int i=n-2;i>=0;i--)
33     {
34         for(int j=0;j<i+1;j++)
35         {
36             sz[i][j]+=max(sz[i+1][j],sz[i+1][j+1]);
37         }
38     }
39     printf("%d
",sz[0][0]);
40 }