ZOJ 1093 Monkey and Banana(动态规划)

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 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 xiyi 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


动态规划的实质是记忆化搜索,也可以写成递推的形式

View Code
 1 # include<stdio.h>
 2 # define MAX 9999999
 3 
 4 int box[100][3];
 5 int height[100];
 6 int number;
 7 
 8 void baifang(int x,int a,int b,int c){
 9     box[x][0] = a;
10     box[x][1] = b;
11     box[x][2] = c;
12 }
13 
14 int DP(int x){
15     if(x>=number) 
16         return 0;
17     if(height[x] != -1)
18         return height[x];
19     int t=0;
20     int i;
21     for(i=1;i<number;i++){
22         if(box[x][0] > box[i][0] && box[x][1] > box[i][1] ||
23             box[x][1] > box[i][0] && box[x][0] >box[i][1])
24             t = DP(i) + box[i][2];
25         if(t>height[x])
26             height[x] = t;
27     }
28     return height[x];
29 }
30 
31 int main(){
32     int n;
33     int cases =1;
34     int a,b,c;
35     while(scanf("%d",&n) && n){
36         box[0][0] = box[0][1] = box[0][2] =MAX;
37         number = 1;
38         int i;
39         for(i=0;i<n;i++){
40             scanf("%d%d%d",&a,&b,&c);
41             baifang(number++,a,b,c);
42             baifang(number++,a,c,b);
43             baifang(number++,b,c,a);
44         }
45         for(i=0;i<number;i++)
46             height[i] = -1;
47         printf("Case %d: maximum height = %d\n",cases++,DP(0));
48     }
49     return 0;
50 }
View Code
 1 # include<stdio.h>
 2 # include<stdlib.h>
 3 
 4 int max(int a,int b)
 5 {
 6     return a>b?a:b;
 7 }
 8 
 9 struct block
10 {
11     int x,y,z;
12 } b[1200];
13 int h[1200];
14 int cmp(const void *a,const void *b)
15 {
16     return ((block *)b)->x - ((block *)a)->x;
17 }
18 
19 int main()
20 {
21     int n;
22     int i,j;
23     int cases =1;
24     int x,y,z,height;
25     while(scanf("%d",&n) && n)
26     {
27         for(i=0; i<n; i++)
28         {
29             scanf("%d%d%d",&x,&y,&z);
30             b[6*i].x   = x;
31             b[6*i].y   = y;
32             b[6*i].z   = z;
33             b[6*i+1].x = x;
34             b[6*i+1].y = z;
35             b[6*i+1].z = y;
36             b[6*i+2].x = y;
37             b[6*i+2].y = x;
38             b[6*i+2].z = z;
39             b[6*i+3].x = y;
40             b[6*i+3].y = z;
41             b[6*i+3].z = x;
42             b[6*i+4].x = z;
43             b[6*i+4].y = x;
44             b[6*i+4].z = y;
45             b[6*i+5].x = z;
46             b[6*i+5].y = y;
47             b[6*i+5].z = x;
48         }
49         qsort(b,6*n,sizeof(b[0]),cmp);
50         h[0] = b[0].z;
51         for (i=1; i<6*n; i++)
52         {
53             height = 0;
54             for (j=0; j<i; j++)
55                 if (b[j].x>b[i].x && b[j].y>b[i].y && h[j]>height)
56                     height = h[j];
57             h[i] = height+b[i].z;
58         }
59 
60         height = h[0];
61         for (i=1; i<6*n; i++)
62             height = max(height,h[i]);
63         printf("Case %d: maximum height = %d\n",cases++,height);
64     }
65     return 0;
66 }

 

 

你可能感兴趣的:(动态规划)