The Tower of Babylon
Time limit1sMemory limit128 MB
Given block types with unlimited copies, each reorientable, find the maximum height of a stack where each block's base is strictly smaller than the one below.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Sorting, Graph
- Solved
- No attempts yet
Problem
The Babylonians had types of blocks, with an unlimited supply of blocks of each type. A type- block is a rectangular solid with side lengths . A block may be reoriented so that any two of its three sides form the base and the remaining side becomes its height.
You want to stack these blocks to build the tallest possible tower. A block may be placed on top of another only if both base side lengths of the upper block are strictly smaller than the corresponding base side lengths of the lower block. In particular, two blocks whose bases have equal dimensions cannot be stacked on each other.
Write a program that determines the height of the tallest tower that can be built from a given set of blocks.
Input
The input consists of one or more test cases. The first line of each test case contains an integer , the number of block types (the maximum value of is 30). Each of the next lines contains three integers , , and .
The input is terminated by a line containing .
Output
For each test case, print one line containing the case number (numbered sequentially starting from 1) and the height of the tallest possible tower, in the format
Case c: maximum height = h