Cow Bowling
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 18487 |
|
Accepted: 12308 |
Description the cows don ' t use actual bowling balls when they go bowling. They 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 one, diagonally adjacent co WS until the "bottom" row is reached. The cow's score is the sum of the numbers of the cows visited along the. The cow with the highest score wins that frame.
Given a triangle with n (1 <= n <=) 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.
Source Usaco 2005 December Bronze
topic Meaning:
The Tower of n rows, starting with a number in the top row, selects only one of the left and right symmetrical two numbers of its next row at a time, adds the numbers, and calculates the maximum sum of the last row.
Problem Solving Ideas:
Simple DP, the equation is Dp[i][j]=a[i][j]+max (dp[i+1][j],dp[i+1][j+1]), calculated from the last line upward.
#include <iostream> #include <cstdio> #include <iomanip> #include <cmath> #include <cstdlib > #include <cstring> #include <map> #include <algorithm> #include <vector> #include <queue
> Using namespace std; #define INF 0x3f3f3f3f #define MAXN int a[maxn][maxn],dp[maxn][maxn];//dp[i][j] denotes the maximum value on column J of row I, int main () {#ifdef Onli
Ne_judge #else freopen ("F:/cb/read.txt", "R", stdin);
Freopen ("F:/cb/out.txt", "w", stdout);
#endif Ios::sync_with_stdio (FALSE);
Cin.tie (0);
int n; while (Cin>>n) {for (int i=0, i<n; ++i) for (int j=0; j<=i; ++j) cin> >a[i][j];//Input memset (dp,0,sizeof (DP));//Initialize for (int. i=0; i<n; ++i) for (int j=0; j<=i; ++J) Dp[i][j]=a[i][j]+max (dp[i+1][j],dp[i+1][j+1]);//Copy a array to DP for (int i=n-1; i>=0;-i)//from the last line Calculate up for (int j=0; j<=i; ++j) Dp[i][j]=a[i][j]+max (dp[i+1][j],dp[i+1][j+1]); /*for (int i=0; i<n; ++i) {for (int j=0; j<=i; ++j) cout<<dp[i][j]<< "
";
cout<<endl;
}*/cout<<dp[0][0]<<endl;
} return 0;
}/* 5 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 * *