HDU 1069 monkey and banana

Source: Internet
Author: User

Description:

Monkey and banana
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3291    Accepted Submission(s): 1706


Problem descriptionA 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 isclever 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 cocould 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. theproblem is that, in building a tower, one block cocould only be placed on top of another block as long asthe two base dimensions of the upper block were both strictly smaller than the corresponding basedimensions of the Lower Block because there has to be some space for the monkey to step on. thismeant, 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 in 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: maximum height = height ". 

 
 
Sample Input1 

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 outputCase 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342 Train of Thought Analysis: For this question, we can first determine a general direction, that is, to first put the square with a large bottom area. There are three methods for each square, so we can store the three states of each square and sort them by area size from large to small. After starting the task, let's do the most important thing: establish the DP equation: we set DP [I] to indicate the maximum height of the whole tower when I brick is used as the top of the whole tower. Then DP [I] = max {DP [J] + RET [I]. z | 0 <= j <I, (Ret [J]. x> RET [I]. X & RET [J]. y> RET [I]. y) | (Ret [J]. y> RET [I]. X & RET [J]. x> RET [I]. Y )}. now we can write the code. Three AC code:
1 # include <iostream> 2 # include <algorithm> 3 using namespace STD; 4 // define struct 5 struct rectangle 6 {7 int x, y, z; 8 int S; 9}; 10 rectangle RET [100]; 11 int DP [100]; 12 int cmp (rectangle A, rectangle B) // sort the area in descending order by 13 {14 return. s> B. s; 15} 16 int main () 17 {18 int N; 19 int x, y, z; 20 int num = 1; 21 While (CIN> N) 22 {23 if (n = 0) break; 24 int I = 0; 25 for (Int J = 0; j <n; j ++) // input data and convert it to 26 {27 Cin> x> Y> Z; // input x, y, Z28 RET [I]. X = x; // use Z as the height 29 RET [I]. y = y; 30 RET [I]. z = z; 31 RET [I]. S = RET [I]. x * RET [I]. y; 32 RET [I + 1]. X = z; // use X as the 33 RET [I + 1] in height. y = y; 34 RET [I + 1]. z = x; 35 RET [I + 1]. S = RET [I + 1]. x * RET [I + 1]. y; 36 RET [I + 2]. X = x; // use y as the height 37 RET [I + 2]. y = z; 38 RET [I + 2]. z = y; 39 RET [I + 2]. S = RET [I + 2]. x * RET [I + 2]. y; 40 I + = 3; 41} 42 sort (Ret, RET + 3 * n, CMP); // sort 43 for (INT I = 0; I <3 * n; I ++) // assign the initial value to its own height 44 DP [I] = RET [I]. z; 45 for (INT I = 1; I <3 * n; I ++) 46 {47 for (Int J = 0; j <I; j ++) 48 if (Ret [J]. x> RET [I]. X & RET [J]. y> RET [I]. y) | (Ret [J]. y> RET [I]. X & RET [J]. x> RET [I]. y) 49 If (DP [J] + RET [I]. z> DP [I]) DP [I] = DP [J] + RET [I]. z; // key of the DP equation 50} 51 int max_height = DP [0]; 52 for (Int J = 0; j <3 * n; j ++) 53 If (DP [J]> max_height) 54 max_height = DP [J]; 55 cout <"case" <<'' <num <': '<"maximum height =" <max_height <Endl; 56 num ++; 57} 58 return 0; 59}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.