JOJ1126 Monkey and Banana

1126 : Monkey and Banana

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 222 94 Standard

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.

Input Specification

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 .

Output Specification

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

This problem is used for contest: 36  


本人对动态规划一直都不在行,虽然经常会遇到,但是真的无法轻易掌握其真谛,寒假要好好努力了。

 

思路:这个是动态规划问题,感觉很类似于0/1背包,就是比较不放和放哪种情况得到的结果最优就用哪种。这个题还有个小点就是对于每一个方块来说着地的面都有三种情况,对于每一种情况又都有横放和竖放的两种情况,着看起来很麻烦,我们不妨把这6种情况分别看成是六种不同的方块来处理就OK了,也可看成三种,对于横放和竖放的情况在DP里考虑。


设DP[i]表示在第i块方块为底能得到的最大高度是多少。用一个blocks[a][3]来存放各个方块。height[i]表示保存得到的DP结果。这样子我们就相当于0/1背包的思路(不完全一样),我们一次将他们放到我们摆好的塔中,如果能得到最优解就留下,否则再拿出去就可以。

 

值得注意的一点是,我们要创造一个边界条件,就是 把地面也看成一块长宽高无限大的方块把它放在blocks的第一个位置里,这样子DP(0)就是结果了。

 

悲剧的是我还是无法把递归式转化成递推式。还太菜。。。

 

我的是0.10s,最快的是0.05s,应该是差在递归上了吧。

 

 

Code:

<textarea cols="50" rows="15" name="code" class="cpp">#include&lt;stdio.h&gt; #define MAX 9999999 int blocks[100][3]; int height[100]; int num; void change(int num,int a,int b,int c) { blocks[num][0]=a; blocks[num][1]=b; blocks[num][2]=c; } int DP(int current) { if(height[current]!=-1) { return height[current]; } int t=0; int i; for(i=1;i&lt;num;i++) { if((blocks[current][0]&gt;blocks[i][0]&amp;&amp;blocks[current][1]&gt;blocks[i][1])||(blocks[current][0]&gt;blocks[i][1]&amp;&amp;blocks[current][1]&gt;blocks[i][0])) { t=DP(i)+blocks[i][2]; } if(t&gt;height[current]) { height[current]=t; } } return height[current]; } int main() { int n; int cases=0; int i; int a,b,c; blocks[0][0]=blocks[0][1]=blocks[0][2]=MAX; while(scanf(&quot;%d&quot;,&amp;n)&amp;&amp;n) { num=1; for(i=0;i&lt;n;i++) { scanf(&quot;%d%d%d&quot;,&amp;a,&amp;b,&amp;c); change(num++,a,b,c); change(num++,a,c,b); change(num++,b,c,a); } for(i=0;i&lt;=num;i++) { height[i]=-1; } printf(&quot;Case %d: maximum height = %d/n&quot;,++cases,DP(0)); } } </textarea>

 

你可能感兴趣的:(c,Integer,input,Build,each,Types)