UVA437「The Tower of Babylon」
题目链接：UVA437「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 (, , ). 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.
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 , and .
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’
显然这是一道 dp 题，因为每个块可以以给出的三条棱中的任意一条作为高，我们不妨将一个块按照高度取不同的棱分为三个块（即将原来的无向块变成有向块）。其次我们注意到，虽然每种有向块都有无穷多个，但是其实一种有向块最多只有一个会出现在塔中（因为要求块底部长宽从下到上严格递增），这样问题就等价于给出 个有向块，求塔的最大高度。如何构造状态以及状态转移方程是重点。定义有向块数组 ，其字段 分别表示有向块的长、宽、高。这里我们考虑顺序遍历所有有向块进行动态规划，则需要将一个有向块的长、宽分别定义为有向块底面 中的较大值和较小值，然后根据先长后宽由大到小对有向块进行排序，这样才能保证最终塔高的最大值一定在顺序遍历有向块过程中出现（因为最终塔一定满足从底块到顶块按长、宽严格递减的条件，而满足这种条件的所有可能塔的构造都在顺序遍历有向块时出现）。构造的状态及状态转移方程如下：
首先构造状态 表示遍历完第 个块后，塔顶为第 个块时塔还能获得的最大高度。