zoukankan      html  css  js  c++  java
  • UVa 437 The Tower of Babylon

     The Tower of Babylon 

    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 tex2html_wrap_inline32 . 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 and Output

    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 tex2html_wrap_inline40 , tex2html_wrap_inline42 and tex2html_wrap_inline44 .

    Input is terminated by a value of zero (0) for n.

    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

    此题属于最长上升子序列问题的变形。

    每次读入一块砖后,把它分成六个角度存储六次(一个长方体的砖会有三个不同的面,每个面做底时都有两种摆放的角度,一共六种),然后按底面的边长从大到小排序,然后用最长上长子序列问题的状态转移方程求解即可,即设dp[i]表示从1到i的砖块可以堆的最高的塔的高度,有:

      dp[i] = max { height[i], dp[j]+height[i] | i两条底边比j的两条底边都短 }

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cstring>
     5 
     6 using namespace std;
     7 
     8 struct BLOCK
     9 {
    10     int x;
    11     int y;
    12     int z;
    13 };
    14 
    15 int n;
    16 int dp[200];
    17 BLOCK b[200];
    18 
    19 bool cmp(BLOCK a,BLOCK b)
    20 {
    21     return a.x>b.x||(a.x==b.x&&a.y>b.y);
    22 }
    23 
    24 int main()
    25 {
    26     int kase=0;
    27 
    28     while(scanf("%d",&n)==1&&n)
    29     {
    30         kase++;
    31 
    32         int t=0,_x,_y,_z;
    33         for(int i=1;i<=n;i++)
    34         {
    35             scanf("%d %d %d",&_x,&_y,&_z);
    36             b[t].x=_x;
    37             b[t].y=_y;
    38             b[t++].z=_z;
    39             b[t].x=_y;
    40             b[t].y=_x;
    41             b[t++].z=_z;
    42             b[t].x=_y;
    43             b[t].y=_z;
    44             b[t++].z=_x;
    45             b[t].x=_z;
    46             b[t].y=_y;
    47             b[t++].z=_x;
    48             b[t].x=_z;
    49             b[t].y=_x;
    50             b[t++].z=_y;
    51             b[t].x=_x;
    52             b[t].y=_z;
    53             b[t++].z=_y;
    54         }
    55 
    56         sort(b,b+t,cmp);
    57 
    58         memset(dp,0,sizeof(dp));
    59 
    60         for(int i=0;i<t;i++)
    61         {
    62             int ans=b[i].z;
    63             for(int j=0;j<i;j++)
    64                 if( ((b[i].x<b[j].x&&b[i].y<b[j].y)||(b[i].x<b[j].y&&b[i].y<b[j].x)) && dp[j]+b[i].z>ans )
    65                     ans=dp[j]+b[i].z;
    66             dp[i]=ans;
    67         }
    68 
    69         int ans=0;
    70         for(int i=0;i<t;i++)
    71             if(dp[i]>ans)
    72                 ans=dp[i];
    73 
    74         printf("Case %d: maximum height = %d
    ",kase,ans);
    75     }
    76 
    77     return 0;
    78 }
    [C++]
  • 相关阅读:
    Linux上安装Oracle 10g 装后感
    分享16个javascript&jQuery的MVC教程
    6个出色的基于JQuery的Tab选项卡实例
    8 个高可用的 jQuery 表单验证插件
    6 套多点触摸屏的手势图标集
    PHP V5.3 中的新特性,第 4 部分: 创建并使用 Phar 归档
    精选15个国外最流行的CSS框架
    PHP V5.3 中的新特性,第 1 部分: 对象接口的变化
    分享10个便利的HTML5/CSS3框架
    XAMPP下pear安装
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3572359.html
Copyright © 2011-2022 走看看