挑战2.3 Cow Bowling(POJ 3176)

Cow Bowling
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14681   Accepted: 9763

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.

题目大意:

给你一个数字三角形,然后输出从a[1][1]到最后一行的某条线路上的最大的和,每次的行走路线只能走向下或者向右的路线。

解题思路:

裸裸的DP模板,数字三角形问题,我用的是从下往上的递推,也就是说,从倒数第n-1行开始,选择一个数字,使得这个数在加上第n行的数字后

他的和在目前这些数字中达到最大。然后每层都采用这样的思路进行循环,最后输出a[1][1]就表示了从第一行的第一个数字到最后一行的某个数字的和

的最大值.

状态:a[i][j]表示的是从第i行第j列的数字到最后一行的某个数字的和的最大值。

状态转移方程:a[i][j]+=max(a[i+1][j],a[i+1][j+1])


代码:

# include
# include
# include
# include
# include
# include
# include
# include
# include
# include

using namespace std;

typedef long long LL;

# define inf 999999999
# define MAX 350+4

int a[MAX][MAX];
LL ans;
int n;

int main(void)
{
    while ( cin>>n )
    {
        getchar();
        for ( int i = 1;i <= n;i++ )
        {
            for ( int j = 1;j <= i;j++ )
            {
                cin>>a[i][j];
            }
        }

        for ( int i = n-1;i >= 1;i-- )
        {
            for ( int j = 1;j <= i;j++ )
            {
                a[i][j]+=max( a[i+1][j],a[i+1][j+1]);
            }
        }

        cout<


你可能感兴趣的:(挑战程序设计板刷)