POJ 3176 (DP Pyramid) _ Dynamic Programming DP

Source: Internet
Author: User
Cow Bowling Time limit:1000ms Memory limit:65536k Total submissions:17271 accepted:11531

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 as this:

          7



        3   8



      8   ,   1 0 2 7 4 4 4 5 2 6 5
Then the other cows traverse the triangle starting the ' its tip and moving ' down ' to one of the two 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 way. The cow with the highest score wins this 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

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.

Source


Click to open the topic link http://poj.org/problem?id=3176


Meaning

Enter an n-tier triangle, the I layer has number I, to find from the 1th layer to the N layer of all the routes, weights and the largest route.

Rule: A number of layer I can only be connected to the I+1 layer and its position adjacent to the two number of one, similar to the Yang Hui's triangle ...

Analysis: Small data, directly using two-dimensional array storage, simple Dp;dp[i][j]=maps[i][j]+max (dp[i+1][j],dp[i+1][j+1]);


#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <queue>
#define INF 0x3f3f3f3f
typedef long LL;
using namespace std;

#define N

Maps[n][n],dp[n][n int];

int main ()
{
    int n,i,j;
    while (scanf ("%d", &n)!=eof)
    {
        memset (maps,0,sizeof (maps));
        Memset (Dp,0,sizeof (DP));
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=i;j++)
            {
                scanf ("%d", &maps[i][j]);
            }

        for (i=n;i>=1;i--)
        {for
            (j=1;j<=i;j++)
            {
                Dp[i][j] = Maps[i][j]+max (dp[i+1][j],dp[i+1][j +1]);
            }
        printf ("%d\n", dp[1][1]);
    return 0;
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.