Welcome

首页 / 软件开发 / 数据结构与算法 / 算法题:UVA 10051 - Tower of Cubes(dp + DAG最长路径)

算法题:UVA 10051 - Tower of Cubes(dp + DAG最长路径)2014-04-12 csdn博客 accelerator_In this problem you are given N colorful cubes each having a distinct weight. Each face of a cube is colored with one color. Your job is to build a tower using the cubes you have subject to the following restrictions:

Never put a heavier cube on a lighter one.

The bottom face of every cube (except the bottom cube, which is lying on the floor) must have the same color as the top face of the cube below it.

Construct the tallest tower possible.

Input

The input may contain multiple test cases. The first line of each test case contains an integer N ( ) indicating the number of cubes you are given. The i­th ( ) of the next N lines contains the description of the i­th cube. A cube is described by giving the colors of its faces in the following order: front, back, left, right, top and bottom face. For your convenience colors are identified by integers in the range 1 to 100. You may assume that cubes are given in the increasing order of their weights, that is, cube 1 is the lightest and cube N is the heaviest.

The input terminates with a value 0 for N.

Output

For each test case in the input first print the test case number on a separate line as shown in the sample output. On the next line print the number of cubes in the tallest tower you have built. From the next line describe the cubes in your tower from top to bottom with one description per line. Each description contains an integer (giving the serial number of this cube in the input) followed by a single white­space character and then the identification string (front, back, left, right, top or bottom) of the top face of the cube in the tower. Note that there may be multiple solutions and any one of them is acceptable.

Print a blank line between two successive test cases.

Sample Input

31 2 2 2 1 23 3 3 3 3 33 2 1 1 1 1101 5 10 3 6 52 6 7 3 6 95 7 3 2 1 91 3 3 5 8 106 6 2 2 4 41 2 3 4 5 610 9 8 7 6 56 1 2 3 4 71 2 3 3 2 13 2 1 1 2 30
Sample Output

Case #122 front3 front Case #281 bottom2 back3 right4 left6 top8 front9 front10 top