Problem
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:
3
8 1
2 7 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
573 88 1 02 7 4 44 5 2 6 5
Sample Output
30
Hint
Explanation of the sample:
*
3
*
8 1
*
2 7 4
*
4 5 2) 6 5
The highest score is achievable by traversing the cows as shown above. Test instructions: Go down or to the right every time, to maximize and analyze: forward: Each step from the top or upper left, Dp[i][j] means the first line J column maximum value dp[i][j]=max{dp[i-1][j],dp[i-1][j-1]}+a[i][j].
#include <stdio.h> #include <algorithm>using namespace Std;int n,a[355][355],ans[355][355],maxans;int Main () { scanf ("%d", &n); for (int i=1;i<=n;i++) for (int j=1;j<=i;j++) scanf ("%d", &a[i][j]); for (int i=1;i<=n;i++) {for (int j=1;j<=i;j++) { Ans[i][j]=max (ans[i-1][j],ans[i-1][j-1]) +a[i][j]; Maxans=max (Ans[i][j],maxans); } } printf ("%d", Maxans); return 0;}
Reverse: Inverse from n-1 line to line 1th, each comparison below and below the right of the size, large plus go, the last output A[1][1] can.
#include <stdio.h> #include <algorithm>using namespace Std;int n,i,j,a[355][355];int main () { scanf (" %d ", &n); for (i=1;i<=n;i++) for (j=1;j<=i;j++) scanf ("%d", &a[i][j]); for (i=n-1;i>=1;i--) for (j=1;j<=i;j++) A[i][j]+=max (a[i+1][j],a[i+1][j+1]); printf ("%d", a[1][1]); return 0;}
"POJ 3176" Cow Bowling