Light OJ 1004-monkey Banana problem (DP)

Source: Internet
Author: User
Tags time limit

Title Link: http://lightoj.com/volume_showproblem.php?problem=1004


1004-monkey Banana problem

PDF (中文版) Statistics Forum
Time Limit:2 second (s) Memory limit:32 MB

Mathematics to solve the great "Monkey Banana problem". It states that, a monkey enters into a diamond shaped the dimensional array and can jump in any of the adjacent cells down from its current position (see figure). While moving from one cell to another, the monkey eats all the bananas kept in that cell. The monkey enters into the array from the upper part and goes out through the lower part. Find the maximum number of bananas the monkey can eat.

Input

Input starts with an integer T (≤50), denoting the number of test cases.

Every case starts with an integer N (1≤n≤100). It denotes that, there'll be 2*n-1 rows. The ith (1≤i≤n) line of next N lines contains exactly i numbers. Then there'll be N-1 lines. The jth (1≤j < N) line contains n-j integers. Each of the are greater than zero and less than 215. Output

For each case, print the case number and maximum number of bananas eaten by the monkey.

Sample Input Output for Sample Input

2

4

7

6 4

2 5 10

9 8 12 2

2 12 7

8 2

10

2

1

2 3

1

Case 1:63

Case 2:5

Note

The Dataset is huge, and the use faster I/O methods.

To find the maximum value from top to bottom path

Parsing: A simple DP


The code is as follows:


#include <iostream> #include <algorithm> #include <map> #include <stack> #include <set> # include<string> #include <cstdio> #include <cstring> #include <cctype> #include <cmath> #
Define N 1009 using namespace std;
const int inf = 0X3F3F3F3F;
const INT mod = 1e9 + 7;
Const double EPS = 1e-8;
Const double PI = ACOs (-1.0);
typedef long Long LL;

int dp[n][n], a[n][n];
    int main () {int T, n, I, j, cnt = 0;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        for (i = 1; I <= n; i++) {for (j = 1; J <= I; j + +) scanf ("%d", &a[i][j]);
        } int k = 2 * n-1, nn = n;
            for (i = n + 1; I <= K; i++) {nn--;
        for (j = 1; J <= nn; j + +) scanf ("%d", &a[i][j]);
        } memset (DP, 0, sizeof (DP));
for (i = 1; I <= N, i++) {for (j = 1; J <= I; j + +) {                DP[I][J] = max (dp[i-1][j-1], dp[i-1][j]) + a[i][j]; }} for (i = n + 1, i <= K; i++) {for (j = 1; J <= N; j + +) dp[i][
        J] = max (dp[i-1][j + 1], dp[i-1][j]) + a[i][j];
    } printf ("Case%d:%d\n", ++cnt, dp[k][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.