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; }