POJ 2241

来源:互联网 发布:剑灵人男捏脸数据图 编辑:程序博客网 时间:2024/06/06 02:34

题目:

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2241

Description

Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story: 
The babylonians had 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 wanted to construct the tallest tower possible by stacking blocks. The problem was 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. 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 babylonians can build with a given set of blocks.

Input

The input 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

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

代码:

<pre name="code" class="html">#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <map>using namespace std;struct  cube{    int x,y,z;};bool cmp(cube a,cube b){    return a.x<b.x;}int main(){    int dp[100],n,j,k=1,i,x,y,z,max1;    cube   a[100];    while(scanf("%d",&n),n)    {        for(i=0;i<n;i++)        {            scanf("%d%d%d",&x,&y,&z);            a[i].x=max(x,y);            a[i].y=min(x,y);            a[i].z=z;            a[i+n].x=max(y,z);            a[i+n].y=min(y,z);            a[i+n].z=x;            a[i+2*n].x=max(x,z);            a[i+2*n].y=min(x,z);            a[i+2*n].z=y;        }        sort(a,a+n*3,cmp);        memset(dp,0,sizeof(dp));        dp[0]=a[0].z;        for(i=1;i<n*3;i++)        {            dp[i]=a[i].z;            for(j=0;j<i;j++)            {                if(a[i].x>a[j].x&&a[i].y>a[j].y)                    dp[i]=max(dp[i],dp[j]+a[i].z);            }         }        max1=dp[0];        for(i=1;i<n*3;i++)            max1=max(max1,dp[i]);  printf("Case %d: maximum height = %d\n",k++,max1);  }return 0;}

反思:其实每个类型的砖块最多用是二次,因为长宽是严格的递减,但是用那两次有三种可能,所以可以把每个类型的砖块找出三个来,并分别确定不同的长宽高,然后对长或者宽进行排序,然后进行动态规划,找出长宽严格递减的最大高度

0 0
原创粉丝点击