Hangzhou Electric oj--1069 Monkey and Banana (Dynamic planning!) )

Source: Internet
Author: User
Monkey and Banana

Problem Description A group of researchers is designing an experiment to test the IQ of a monkey. They 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 being able to reach the banana by placing one block on the top another to build a to Wer and climb up to get their favorite food.

The researchers has n types of blocks, and an unlimited supply of blocks of each type. Each type-i block is a rectangular solid with linear dimensions (xi, Yi, zi). A Block could is reoriented so it three dimensions determined the dimensions of the base and the other Di Mension was the height.

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

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

Input the input file would contain one or more test cases. The first line of all 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 is 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 = 2:maximum height = max Case 3:maximum height = $ case 4:maximum height = 342

The topic is not difficult. For the dynamic programming problem, I think the most important thing is to find the so-called recursive equation.

This is the most important. Finding the recursive equation, the code implementation is only a matter of time.

There are some points for this topic to be grasped.

1, the next layer of the area to be strictly greater than the previous layer. I've been wrong here a few times. The so-called strict greater than, that is, the next layer of the length of the width is greater than the next layer of the long or wide, equal to not.

2, this question is more like the 01 knapsack problem. The method I use is split. A block will be split into 3 types. (a block has three positions, and a block is placed at most of these three positions) so, first sort by area. And then move back again. Can reduce the complexity of time.

3,hdp[i] Indicates the maximum height that can be reached if block I blocks is the top.

Send the AC code.

#include <iostream> #include <vector> #include <algorithm> #include <cstdio> using namespace std

;
       struct block{int area;
	   int height;
	   int width;
       int length;
       
};
 BOOL CMP (block &a,block &b) {return a.area>b.area;
	} int main () {int kinds;
	int I,j,l,w,h,s=1;
	int Max;
    int k;
    Block b[100];
    
    Vector<int> HDP (300);
    Block temp;
	  while (1) {k=0;
      cin>>kinds;
      if (kinds==0) break;
	  for (i=0;i<kinds;i++) {cin>>l>>w>>h;
      Temp.width=l;temp.length=w; Temp.area=l*w;
      Temp.height=h;
      B[k++]=temp;
      Temp.width=l;temp.length=h; Temp.area=l*h;
      Temp.height=w;
      B[k++]=temp;
      Temp.width=h;temp.length=w; Temp.area=h*w;
      Temp.height=l;
      B[k++]=temp; }//Will be wrong. Strictly small, that is, each side (length and width) is smaller than the bottom.
       is not equal to.
        Sort (b,b+kinds*3,cmp);//Sort by area from large to small. 
     hdp[0]=b[0].height;//if the maximum floor area of the brick is the bottom, then the highest height is its height  The next step is dynamic planning.
        The equation of motion is hdp[i]=max{hdp[j]+b[i].height,b[i].height|b[j].area>b[i].area,j=0,1,...... i-1};
         for (i=1;i<kinds*3;i++) {hdp[i]=b[i].height;
		 for (j=0;j<i;j++)//If the area is greater than B[i].area, then the test instructions must not be satisfied. {if (b[i].width<b[j].width && b[i].length<b[j].length) | |
			(B[i].width<b[j].length && b[i].length<b[j].width))
		 if (Hdp[j]+b[i].height>hdp[i])//Find the maximum value hdp[i]=hdp[j]+b[i].height;
		}} max=0;
      for (i=0;i<kinds*3;i++) if (Hdp[i]>max) max=hdp[i];
	  printf ("Case%d:maximum height =%d\n", S,max);
     s++;
} return 0;
 }

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.