标签:高度 Dimension ref sam 结构体 code linear lines sdn
动规题一直似懂非懂,今天做了道经典例题,加深理解。
下面给出原题:
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.
InputThe 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.
OutputFor 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
110 20 3026 8 105 5 571 1 12 2 23 3 34 4 45 5 56 6 67 7 7531 41 5926 53 5897 93 2384 62 6433 83 27 0
Sample Output
Case 1: maximum height = 40Case 2: maximum height = 21Case 3: maximum height = 28Case 4: maximum height = 342
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 struct brick{ 6 int l=0,w=0,h=0; //长宽高 7 }b[1000]; //砖块结构体数组 8 int dp[1000]; //dp[i]: 以第i个砖块为最底层可得到的最高高度 9 10 bool cmp(brick b1,brick b2){ //先长后宽从小到大排序 11 if(b1.l==b2.l) return b1.w<b2.w; 12 return b1.l<b2.l; 13 } 14 15 int main() 16 { 17 int n; 18 int x,y,z; 19 int kase = 0; 20 while(cin>>n && n){ 21 int len = 0; 22 for(int i = 0; i < n; ++i){ 23 cin>>x>>y>>z; 24 //每个砖块有三种放置方法(考虑长>宽) 25 b[len].h = x,b[len].l = y>z?y:z,b[len++].w = y>z?z:y; 26 b[len].h = y,b[len].l = x>z?x:z,b[len++].w = x>z?z:x; 27 b[len].h = z,b[len].l = x>y?x:y,b[len++].w = x>y?y:x; 28 } 29 sort(b,b+len,cmp); //以先长后宽从小到大的顺序给砖块排序 30 dp[0] = b[0].h; //现在的b[0]是最小的砖块,上面不能放置其他砖块,高度为它本身 31 int max_h; 32 for(int i=1;i<len;++i){ //从第二个砖块开始循环,每次记录最大值 把第i个箱子放在前i-1个箱子的某一个下面 33 max_h = 0; //前面0到i-1个砖块可以堆放的最高高度 34 for(int j=0;j<i;++j){ 35 if(b[j].l<b[i].l && b[j].w<b[i].w) 36 max_h = max_h>dp[j]?max_h:dp[j]; 37 } 38 dp[i] = b[i].h + max_h; //以第i个砖块为最底层可得到的最高高度=前i-1层可堆放的最高高度+i本身高度 39 } 40 sort(dp,dp+len); 41 cout<<"Case "<<(++kase)<<": maximum height = "<<dp[len-1]<<endl; 42 } 43 44 return 0; 45 46 }
参考题解:
【动态规划】Problem-1069 Monkey and Banana
标签:高度 Dimension ref sam 结构体 code linear lines sdn
原文地址:https://www.cnblogs.com/Aikoin/p/10061206.html