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 5Then the other cows traverse the triangle starting fromIts tip and moving" Down"To one of the diagonally adjacent cows until the"Bottom"Row isReached. 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
12.. n+1: Line i+1 contains i space-separated integers that represent row I of the triangle.
Output
1 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 is as shown above.
Source
Usaco 2005 December Bronze
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 #defineN 3566 intMp[n][n];7 intDp[n][n];8 intMain ()9 {Ten intN; One while(SCANF ("%d", &n) = =1){ AMemset (DP,0,sizeof(DP)); - for(intI=1; i<=n;i++){ - for(intj=1; j<=i;j++){ thescanf"%d",&mp[i][j]); - } - } -dp[1][1]=mp[1][1]; + for(intI=2; i<=n;i++){ - for(intj=1; j<=i;j++){ +Dp[i][j]=max (dp[i-1][j-1]+mp[i][j],dp[i-1][j]+mp[i][j]); A } at } - intmaxn=-1; - for(intI=1; i<=n;i++){ -maxn=Max (maxn,dp[n][i]); - } -printf"%d\n", MAXN); in } - return 0; to}
View Code
POJ 3176 Cow Bowling (DP base)