乐逍遥xwl

导航

POJ 3176 Cow Bowling (数塔DP)

Cow Bowling

Description

The cows don't use actual bowling balls when 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 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.
 
思路:数塔DP,DP入门题......
 
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<queue>
 9 using namespace std;
10 #define ll long long 
11 const int inf=1e9+7;
12 const int mod=1e9+7;
13  
14 const int maxn=400+5;
15 
16 int dp[maxn][maxn];
17 
18 int main()
19 {
20     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
21     
22     int n;
23     cin>>n;
24     
25     for(int i=1;i<=n;i++)
26     {
27         for(int j=1;j<=i;j++)
28         {
29             cin>>dp[i][j];//读入数塔 
30         }
31     }
32     
33     for(int i=n-1;i>=1;i--)
34     {
35         for(int j=1;j<=i;j++)
36         {
37             dp[i][j]+=max(dp[i+1][j],dp[i+1][j+1]);
38         }
39      } 
40     
41     cout<<dp[1][1]<<endl;
42     
43     return 0;
44 }

 

 

posted on 2019-07-09 19:18  乐逍遥xwl  阅读(136)  评论(0编辑  收藏  举报