HDU - 1069 Monkey and Banana
Monkey and Banana
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15974 Accepted Submission(s): 8477
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).
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
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.
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.
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
题解:
该题的意思是 给你n个类型的长方体,能将这些长方体堆多高。限制条件是,堆长方体时上面那个长方体必须小于
下面的长方体,即上面长方体长和宽都必须小于下面长方体的长和宽,问最高能堆多少米。
这道题个人感觉跟另外一道DP—嵌套矩形 很相似,如果做过嵌套矩形那道题,则该题稍作转化就能出来结果。
嵌套矩形请参考:http://www.cnblogs.com/creativepower/p/7324575.html
该题类型为DAG问题(有向无环图)。
矩形的放置有6种情况,(长宽)(长高)(宽高)(宽长)(高长)(高宽)
判断该长方体能不能放置在另一个长方体上的条件是长和宽都必须小于另一个长方体的长和宽
即(s[i].x > s[j].x && s[i].y > s[j].y)
如果存在这种情况,则就要对该i的值进行更新
1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 typedef long long LL; 6 struct node { 7 int x, y, z;//x,y表示长方体的长 宽, z表示高 8 }s[200]; 9 bool cmp(node a, node b) { //根据node中的x从小到大排序,如果x一样,则根据y从小到大排序 10 if(a.x == b.x) return a.y < b.y ; 11 return a.x < b.x; 12 } 13 14 void init(int *dp, int n) { //初始化dp数组,存入每个对应长方体的高度 15 for(int i = 0 ; i < n; i++){ 16 dp[i] = s[i].z; 17 } 18 } 19 20 int main() 21 { 22 int dp[200]; 23 int n, a[6]; 24 int E, ans , k = 1; 25 while(cin >> n && n) { 26 ans = 0; 27 E = 0;//总共有多少种情况 28 while(n--){//长方体可以分为6种情况进行储存 29 cin >> a[0] >> a[1] >> a[2]; 30 s[E].x = a[0], s[E].y = a[1], s[E++].z = a[2]; 31 s[E].x = a[0], s[E].y = a[2], s[E++].z = a[1]; 32 s[E].x = a[1], s[E].y = a[2], s[E++].z = a[0]; 33 s[E].x = a[1], s[E].y = a[0], s[E++].z = a[2]; 34 s[E].x = a[2], s[E].y = a[1], s[E++].z = a[0]; 35 s[E].x = a[2], s[E].y = a[0], s[E++].z = a[1]; 36 } 37 sort(s, s + E, cmp);//将储存的边进行由小到大进行排序 38 init(dp, E); 39 for(int i = 1; i < E; i++) {//进行判断的情况 40 for(int j = 0; j < i ; j++) { 41 if(s[i].x > s[j].x && s[i].y > s[j].y){ 42 if(dp[i] < dp[j] + s[i].z) 43 dp[i] = dp[j] + s[i].z; 44 } 45 } 46 } 47 for(int i = 0 ; i < E; i++) {//找出最高的情况 48 ans = ans < dp[i] ? dp[i] : ans; 49 } 50 cout << "Case " <<k++<<": maximum height = "; 51 cout << ans << endl; 52 } 53 return 0; 54 }