Monkey and Banana Time Limit: 2 Seconds Memory Limit: 65536 KB
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 haveNTypes of blocks, and an unlimited supply of blocks of each type. Each type-IBlock 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. the problem is that, in building a tower, one block cocould 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 SpecificationThe 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
NIs 30.
Each of the next
NLines contains three integers representing the values
Xi,
YiAnd
Zi.
Input is terminated by a value of zero (0)
N. Output SpecificationFor 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 270
Sample Output
Case 1: maximum height = 40Case 2: maximum height = 21Case 3: maximum height = 28Case 4: maximum height = 342
N bricks are stacked to obtain the maximum heap height. The condition is that the two sides of the bricks must be smaller than the following bricks, give the monkey a position to climb.
Ideas:
This is a typical dp problem in a DAG. Because each type of bricks is infinite and can be freely placed, you can consider the three placement forms of each brick as one, there are a total of 3 N bricks,
This problem is similar to the rectangular nesting problem, that is, finding the maximum heap height (excluding I bricks) from number I, dp (I) = max (dp (j) + j Height; j is the number of bricks that can be placed on I). The algorithm design is not difficult, mainly because of many details. For example, the maximum height of the starting node is infinite, the result is dp (0 );
The Code is as follows:
#include
#include
#include
using namespace std;int box[100][3];int height[100];int number;
// Convert bricks into three types: void change (int index, int a, int B, int c) {box [index] [0] =; box [index] [1] = B; box [index] [2] = c ;}
// Dpint DP (int j) {int & ans = height [j]; // if (ans! =-1) return ans;
// Search for each layer for (int I = 1; I <= number; I ++) {if (I! = J) {if (box [I] [0] <box [j] [0] & box [I] [1] <box [j] [1]) | (box [I] [1] <box [j] [0] & box [I] [0] <box [j] [1]) {int temp = DP (I) + box [I] [2]; if (temp> ans) ans = temp ;}}}
// The last layer is not updated. If (ans =-1) ans = 0; return ans;} int main () {int n; int Case = 0; while (cin> n & n) {int a, B, c; number = 0; // The floor is regarded as an infinite brick. Box [0] [0] = box [0] [1] = box [0] [2] = INT_MAX; for (int I = 1; I <= n; I ++) {cin> a> B> c; change (++ number, a, B, c); change (++ number, B, c, a); change (++ number, c, a, B) ;}for (int I = 0; I <= number; I ++) {height [I] =-1 ;}cout <"Case" <++ Case <": maximum height =" <DP (0) <endl ;} return 0 ;}