Cow Bowling
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K(Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 4
Problem Description
The cows don't use actual bowling balls when they go bowling. Theyeach take a number (in the range 0..99), though, and line up in astandard 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 andmoving "down" to one of the two diagonally adjacent until the "bottom" row is reached. The cow'sscore is the sum of the numbers of the cows visited along the way.The cow with the highest score wins thatframe.
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 thatrepresent 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
Source
PKU
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K(Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 4
Problem Description
The cows don't use actual bowling balls when they go bowling. Theyeach take a number (in the range 0..99), though, and line up in astandard 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 andmoving "down" to one of the two diagonally adjacent
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 thatrepresent 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
Source
PKU
C++ source code:
#include<iostream>
using namespace std;
int main()
{
int tree[350][350],max,a,b,n,i,j;
while(cin>>n)
{
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
cin>>tree[i][j];
}
for(i=n-2;i>=0;i--) //数字从底部开始相加
{
for(j=0;j<=i;j++)
{
a=tree[i][j]+tree[i+1][j];
b=tree[i][j]+tree[i+1][j+1];
a>b?tree[i][j]=a:tree[i][j]=b;
}
}
cout<<tree[0][0]<<endl;//此时tree[0][0]就是我们所要求的最大值。
}
}