Light OJ 1004-monkey banana problem DP Solution

Source: Internet
Author: User

1004-monkey banana problem
PDF (English) Statistics Forum
Time Limit: 2 second (s) Memory limit: 32 MB

You are in the world of mathematics to solve the great "monkey banana problem". It states that, a monkey enters into a diamond shaped two dimen=array and can jump in any of the adjacent cellsDownFrom 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 integerT (≤ 50), Denoting the number of test cases.

Every case starts with an integerN (1 ≤ n ≤ 100). It denotes that, there will be2 * n-1Rows.Ith (1 ≤ I ≤ n)Line of nextNLines contains exactlyINumbers. Then there will beN-1Lines.Jth (1 ≤ j <n)Line containsN-JIntegers. Each number is greater than zero and less215.

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



The enhanced version of the triangle tower question. This is the combination of the top and bottom two inverted triangles.

As a result, I did not pay attention to the subscripts. It took a long time to debug the subscripts and repeatedly emphasized the accuracy of the subscripts.

The method is also from the bottom up: when there is only one array, it is bound to select the largest number from it. When there are two arrays, it is from the first to the next, the number of cells in the current array can go to the number of the next array. Here there are two options, and the maximum value adds up. In this way, the result can be deduced from the bottom up.


Don't ask me what is the use of the algorithm:

Just entertain yourself.

Small companies cannot use algorithms at all, or they do not have people who understand algorithms at all. In this case, even less people who understand algorithms come out as interviewers and recruit people who understand algorithms.

The algorithm is a Dragon Sword, and the problem is that you need to find the dragon first.


# Include <stdio. h> # include <vector> # include <string. h> # include <algorithm> # include <iostream> # include <string> # include <limits. h >#include <stack >#include <queue >#include <set> # include <map> using namespace STD; const int max_n = 100; const int max_dn = 200; long long arr [max_dn] [max_n], TBL [max_n]; long getmostbanana (long a [] [max_n], long DP [], int N) {int Len = (n <1)-1; memset (DP, 0, size Of (long) * n); DP [0] = A [len-1] [0]; for (INT I = len-2, D = 2; I> = n-1; I --, d ++) // note that not I> = N is I> = n-1 {DP [D-1] = DP [D-2] + A [I] [D-1]; for (Int J = D-2; j> 0; j --) {DP [J] = max (DP [J], DP [J-1]) + A [I] [J];} DP [0] = DP [0] + A [I] [0];} For (INT I = n-2, D = n-1; i> = 0; I --, d --) // cannot be I = n-1, To I = n-2 {// a fixed joint is wrong, the answer will be wrong. For (Int J = 0; j <D; j ++) {DP [J] = max (DP [J], DP [J + 1]) + A [I] [J] ;}} return DP [0] ;}int main () {int t, n, T = 1; scanf ("% d ", & T); While (t --) {printf ("case % d:", t ++); scanf ("% d", & N ); for (INT I = 1; I <= N; I ++) {for (Int J = 0; j <I; j ++) scanf ("% LLD ", & arr [I-1] [J]);} For (INT I = n-1, D = N; I> = 1; I --, d ++) {for (Int J = 0; j <I; j ++) scanf ("% LLD", & arr [d] [J]);} printf ("% LLD \ n", getmostbanana (ARR, TBL, n);} return 0 ;}




Light OJ 1004-monkey banana problem DP Solution

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.