POJ 3176 Cow Bowling: Simple DP

Source: Internet
Author: User
Tags printf time limit

Cow Bowling

http://poj.org/problem?id=3176

Time limit:1000ms

Memory limit:65536k

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 Triangl E.

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

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

The highest score is achievable by traversing the cows as shown.

Source

Usaco December Bronze

Memory Search:

/*47ms,1356kb*/
    
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
using namespace Std;  
const int MAXN = 355;  
    
int A[MAXN][MAXN], dp[maxn][maxn], N;  
    
int f (int i, int j)  
{  
    if (Dp[i][j] >= 0) return dp[i][j];  
    if (i = = n-1) return dp[i][j] = a[i][j];  
    return dp[i][j] = A[i][j] + max (f (i + 1, j), F (i + 1, j + 1));  
}  
    
int main ()  
{  
    int i, J;  
    scanf ("%d", &n);  
    for (i = 0; i < n; ++i) for  
        (j = 0; J <= i; ++j)  
            scanf ("%d", &a[i][j]);  
    Memset (DP,-1, sizeof (DP));  
    printf ("%d", f (0, 0));  
    return 0;  
}

Recursive push:

/*16ms,868kb*/
    
#include <cstdio>  
#include <algorithm>  
using namespace std;  
const int MAXN = 355;  
    
int A[MAXN][MAXN];  
    
int main ()  
{  
    int n, I, J;  
    scanf ("%d", &n);  
    for (i = 0; i < n; ++i) for  
        (j = 0; J <= i; ++j)  
            scanf ("%d", &a[i][j]);  
    for (i = n-2 i >= 0;-i)  
        for (j = 0; J <= i; ++j)  
            a[i][j] + = max (a[i + 1][j), A[i + 1][j + 1]);  
    printf ("%d", a[0][0]);  
    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.