程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Light OJ 1004 - Monkey Banana Problem dp題解

Light OJ 1004 - Monkey Banana Problem dp題解

編輯:C++入門知識

Light OJ 1004 - Monkey Banana Problem dp題解


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 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)<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPi4gSXQgZGVub3RlcyB0aGF0LCB0aGVyZSB3aWxsIGJlIDxzdHJvbmc+MipOIC0gMTwvc3Ryb25nPiByb3dzLiBUaGUgPHN0cm9uZz5pPHN1cD50aDwvc3VwPiAoMSCh3CBpIKHcIE4pPC9zdHJvbmc+IGxpbmUgb2YgbmV4dCA8c3Ryb25nPk48L3N0cm9uZz4gbGluZXMgY29udGFpbnMgZXhhY3RseSA8c3Ryb25nPmk8L3N0cm9uZz4gbnVtYmVycy4KIFRoZW4gdGhlcmUgd2lsbCBiZSA8c3Ryb25nPk4gLSAxPC9zdHJvbmc+IGxpbmVzLiBUaGUgPHN0cm9uZz5qPHN1cD50aDwvc3VwPiAoMSCh3CBqIDwgTik8L3N0cm9uZz4gbGluZSBjb250YWlucyA8c3Ryb25nPk4gLSBqPC9zdHJvbmc+IGludGVnZXJzLiBFYWNoIG51bWJlciBpcyBncmVhdGVyIHRoYW4gemVybyBhbmQgbGVzcyB0aGFuIDxzdHJvbmc+MjxzdXA+MTU8L3N1cD48L3N0cm9uZz4uPC9wPgo8aDE+T3V0cHV0PC9oMT4KPHA+Rm9yIGVhY2ggY2FzZSwgcHJpbnQgdGhlIGNhc2UgbnVtYmVyIGFuZCBtYXhpbXVtIG51bWJlciBvZiBiYW5hbmFzIGVhdGVuIGJ5IHRoZSBtb25rZXkuPC9wPgo8dGFibGUgYm9yZGVyPQ=="1" cellspacing="0" cellpadding="0">

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




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved