Light OJ 1004 - Monkey Banana Problem dp题解

本文探讨了一个关于猴子在钻石形状二维数组中跳跃并吃掉所有香蕉的问题,目标是找到猴子能吃到的最大香蕉数量。通过从数组底部向上计算,利用动态规划的方法解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1004 - Monkey Banana Problem
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 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 will be 2*N - 1 rows. The ith (1 ≤ i ≤ N) line of next N lines contains exactly i numbers. Then there will be N - 1 lines. The jth (1 ≤ j < N) line contains N - j integers. Each number is 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



三角形塔题目的加强版,这次是上下两个颠倒的三角形叠加一起了。

结果我下标没注意,要调试了很长时间,反复强调下标的精确性了。

方法也是从底往上:只有一条数组的时候,必然是从中选择最大的一个数,有两条数组的时候,那么就是从上一条走到下一条,当前数组的一个格子的数选择可以走到下一个数组的数,这里是两个选择,的最大值加起来。如此从底往上推导,就能出结果了。


不要问我算法有什么用了:

就自娱自乐吧。

小公司是根本用不上的,或者说他们根本没有懂算法的人在里面,也就更加不会有懂算法的人出来当面试官,招懂算法的人了。

算法是屠龙刀,问题是先要找到龙。


#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 long getMostBanana(long long A[][MAX_N], long long dp[], int n)
{
	int len = (n<<1)-1;
	memset(dp, 0, sizeof(long long)*n);
	dp[0] = A[len-1][0];
	for (int i = len-2, d = 2; i >= n-1; i--, d++)//注意不是i>=n是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--)//不能是i=n-1,要i=n-2
	{//一点定的接合不对,就会答案错误。
		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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值