Link: poj 3176 cow Bowling
Cow Bowling
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:14044 |
|
Accepted:9310 |
Description The cows don't use actual bowling ballwhen 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 rulesSample Input 573 88 1 02 7 4 44 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 |
Question:
From the first layer of the Data tower to the bottom layer, but only along the diagonal line, find the maximum value of the sum of the number in the path.
Analysis:
From the bottom layer to the top, the sum in the Path depends on the size of the following two numbers. Therefore, the top-up method is used to step up and go to the top layer. The maximum sum is selected each time, so that the final result is saved to the top layer.
State transition equation: DP [I] [J] = max (DP [I + 1] [J], DP [I + 1] [J + 1]) + A [I] [J];
Code:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAX_N = 400;int dp[MAX_N][MAX_N];int N, A[MAX_N][MAX_N];void solve() { memset(dp, 0, sizeof(dp)); for(int i = 1; i <= N; i++) dp[N][i] = A[N][i]; for(int i = N-1; i >= 1; i--) for(int j = 1; j <= i; j++) dp[i][j] = A[i][j]+max(dp[i+1][j], dp[i+1][j+1]); printf("%d\n", dp[1][1]);}int main() { while(~scanf("%d", &N)) { for(int i = 1; i <= N; i++) for(int j = 1; j <= i; j++) scanf("%d", &A[i][j]); solve(); } return 0;}
Poj 3176 cow bowling tower problem DP