(Lightoj 1004) Monkey Banana problem simple DP

Source: Internet
Author: User

You areinchThe world's mathematics to solve the great"Monkey Banana Problem". It states that, a monkey enters into a diamond shaped the dimensional array and can jumpinchAny of the adjacent cells down fromIts current position (see figure). While moving fromOne cell to another, the monkey eats all the bananas keptinchThat cell. The monkey enters into the array fromThe upper part and goes outthrough the lower part. Find the maximum number of bananas the monkey can eat. Inputinput starts with an integer T (≤ -), denoting the number of test cases. Every CaseStarts with an integer N (1≤n≤ -). It denotes that, there'll be2*n-1Rows. The ith (1≤i≤n) Line of next N lines contains exactly I numbers. Then there'll be N-1Lines. The jth (1≤j < N) line contains n-j integers. Each number isGreater than zero and less than215. Outputfor each Case, Print the CaseNumber and maximum number of bananas eaten by the monkey. Sample InputOutput forSample Input2476 42 5 Ten9 8  A 22  A 78 2Ten212 31 Case1: the Case2:5

Test instructions from top to bottom, make and Max

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<math.h>#include<queue>#definell Long Long#defineINF 0x3f3f3f3f#defineMet (A, b) memset (A,b,sizeof (a));#defineN 511using namespacestd;intDp[n][n],a[n][n];intMain () {intt,n,con=1; scanf ("%d",&t);  while(t--) {scanf ("%d",&N);  for(intI=1; i<=n; i++)        {             for(intj=1; j<=i; J + +) scanf ("%d",&A[i][j]); }         for(inti=n+1; i<2*n; i++)        {             for(intj=1; j<=2*n-i; J + +) scanf ("%d",&A[i][j]); } Met (DP,0); dp[1][1]=a[1][1];  for(intI=2; i<=n; i++)        {             for(intj=1; j<=i; J + +) {Dp[i][j]=max (dp[i][j],dp[i-1][j-1]+A[i][j]); DP[I][J]=max (dp[i][j],dp[i-1][j]+A[i][j]); }        }         for(inti=n+1; i<2*n; i++)        {             for(intj=1; j<=2*n-i; J + +) {Dp[i][j]=max (dp[i][j],dp[i-1][j]+A[i][j]); DP[I][J]=max (dp[i][j],dp[i-1][j+1]+A[i][j]); }} printf ("Case %d:%d\n", con++,dp[2*n-1][1]); }    return 0;}

(Lightoj 1004) Monkey Banana problem simple DP

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.