HDU 1069—— Monkey and Banana——————【dp】

Monkey and Banana
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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

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

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
 
 
 
题目大意:给你n种长方体木块,每种无限个,给你长宽高,木块可以翻转,即可以将不同的面作为底面,现在让你将这些木块摞起来,问你最高能摞多高。一个木块可以放在另一个木块上的条件是,上面的木块的长、宽小于下面木块的长、宽。
 
解题思路:开始写得很麻烦,还错了,然后参考了别人的思想,先将底面以长为条件排序,如果长度相同,按照宽从小到大排序。定义dp[i]表示,以i为底座时,这些木块最高可以摞多高。
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 1000;
struct Cube{
    int a,b,h;
}cube[maxn];
int dp[maxn];
bool cmp(Cube a,Cube b){
    if(a.a == b.a)
        return a.b < b.b;
    return a.a < b.a;
}
int main(){
    int n,cnt = 0;
    while(scanf("%d",&n)!=EOF&&n){
        memset(dp,0,sizeof(dp));
        for(int i = 1; i <= 3*n; i+=3){
            int aa,bb,cc;
            scanf("%d%d%d",&aa,&bb,&cc);
            for(int j = 0; j < 3; j++){
                if(j == 1)
                    swap(aa,cc);
                if(j == 2)
                    swap(bb,cc);
                cube[i+j].a = aa;
                cube[i+j].b = bb;
                if(cube[i+j].a > cube[i+j].b){
                    swap(cube[i+j].a,cube[i+j].b);
                }
                cube[i+j].h = cc;
            }
        }
        sort(cube+1,cube+1+3*n,cmp);
        for(int i = 1; i <= 3*n;i ++){
            dp[i] = cube[i].h;
        }
        for(int i = 1; i <= 3*n; i++){
            for(int j = i+1; j <= 3*n; j++){
                if(cube[j].a > cube[i].a && cube[j].b > cube[i].b){
                    dp[j] = max(dp[j],dp[i]+cube[j].h);
                }
            }
        }
        int ans = 0;
        for(int i = 1; i <= 3*n;i++)
            ans = max(ans,dp[i]);
        printf("Case %d: maximum height = %d\n",++cnt,ans);
    }
    return 0;
}

  

 
posted @ 2015-12-25 21:17  tcgoshawk  阅读(261)  评论(1编辑  收藏  举报