贪心结合动态规划-POJ-1069-Monkey and Banana

Monkey and Banana

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5395    Accepted Submission(s): 2765

Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.

The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height. 

They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn’t be stacked. 

Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.

 

Input The input file will contain one or more test cases. The first line of each test case contains an integer n,

representing the number of different blocks in the following data set. The maximum value for n is 30.

Each of the next n lines contains three integers representing the values xi, yi and zi.

Input is terminated by a value of zero (0) for n.

 

Output For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format “Case case: maximum height = height”.

 

Sample Input

1 10 20 30 2 6 8 10 5 5 5 7 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 5 31 41 59 26 53 58 97 93 23 84 62 64 33 83 27 0  

Sample Output

Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342  

题目大意:

有一堆箱子,有长宽高,x,y,z。  规定:放在上面的箱子,无论长和宽都要比下面的箱子大,只有一边大是不行的。

箱子的方向可以任意放。 这样,x,y,z就有六种组合。其实也可以说是三种。

因为要和上面的箱子比较,这里为了计算方便,直接写成六种。 

求的是最大高度。

考虑到必须的大箱子在下面。这个其实和 最大递增子段和 问题一个性质。

不了解的朋友看这里:http://blog.csdn.net/gaotong2055/article/details/8985452


只不过这个情况,我们需要先排下序。把大的箱子放前面。有点牵强的说,也算是用了贪心的思想。

虽然我们不能确定一个x边最大的箱子不一定就能放在最下面,例如的它的y边很小,但是,它肯定也放不到其它箱子的上面,所以,把它放前面也是可以的。

因此可以按x边排序。不过在最后比较的时候,要严格遵循 x1 > x2 && y1 > y2。

下面的代码,没有对长 宽 高相等的情况做优化,毕竟数据量不算很大。

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

struct Node {
	int x, y, z;
};

Node blocks[100];
int opt[100];
int a, b, c;

bool cmp(const Node & a, const Node & b) {
	if (a.x == b.x)
		return a.y > b.y;
	else
		return a.x > b.x;
}


int main() {
	int n;
	int cnt = 0;
	while (cin >> n, n) {
		cnt++;
		for (int i = 0; i < 6* n; i += 6) {
			cin >> a >> b >> c;
			blocks[i].x = a;
			blocks[i].y = b;
			blocks[i].z = c;

			blocks[i + 1].x = b;
			blocks[i + 1].y = c;
			blocks[i + 1].z = a;

			blocks[i + 2].x = c;
			blocks[i + 2].y = a;
			blocks[i + 2].z = b;

			blocks[i+3].x = a;
			blocks[i+3].y = c;
			blocks[i+3].z = b;

			blocks[i + 4].x = b;
			blocks[i + 4].y = a;
			blocks[i + 4].z = c;

			blocks[i + 5].x = c;
			blocks[i + 5].y = b;
			blocks[i + 5].z = a;

		}

		sort(blocks, blocks + 6 * n, cmp);
		for (int i = 0; i < 6 * n; i++) {
			opt[i] = blocks[i].z;
			for (int j = 0; j < i; j++) {
				if ( (blocks[i].x < blocks[j].x && blocks[i].y < blocks[j].y)) {
					if (opt[i] < opt[j] + blocks[i].z) {
						opt[i] = opt[j] + blocks[i].z;
					}
				}

			}
		}

		int max = 0;
		for (int i = 0; i < 6 * n; i++) {
			if (opt[i] > max)
				max = opt[i];
		}

		cout <<"Case " << cnt <<": maximum height = " << max << endl;

	}

	return 0;
}

    原文作者:动态规划
    原文地址: https://blog.csdn.net/gladyoucame/article/details/8995610
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞