Algorithm title: UVA 10051

Source: Internet
Author: User

In this problem your are given N colorful cubes each has a distinct weight. Each face of the A cube is colored with one color. Your job is to build a tower using the cubes your have subject to the following:

Never put a heavier cube on a lighter one.

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

Construct the tallest tower possible.

Input

The input may contain multiple test cases. The ' a ' of each test case contains a integer N () indicating the number of cubes you are given. The ith ( ) of the next N lines contains the description of the ith cube. A Cube is described by giving the colors of it faces in the following Order:front, back, left, right, top and bottom fac E. For your convenience colors are identified through integers in the range 1 to 100. You may assume this cubes are given in the increasing order of their weights, which is, Cube 1 is the lightest and cube N I s the heaviest.

The input terminates with a value of 0 for N.

Output

For each test case in the input I 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 for you have built. From the "next line" describe the "cubes in your Tower" to "bottom with one description". Each description contains an integer (giving the serial number of this cube into the input) followed by a single whitespace Character and then the identification string (front, back, left, right, top or bottom) of the "top" face of the The "Cube" in the Tower. Note this there may to multiple solutions and any one of the them is acceptable.

Print a blank line between two successive test cases.

Sample Input

3
1 2 2 2 1 2 3 3 3 3 3 3 3 2 1 1 1 1 1 5 3 6 5 2 6 7 3 6 9 5 7 3 2 1 9
6 6 2 2 4 4 1 2 3 4 5 6 9 8 7 6 5 6 1 2 3 4 7 1 2 3 3 2 1 3 2 1 1 2-3

  

Sample Output

Case #1
2
2 front
3 front case
 
#2
8
1 Bottom
2 back
3 right
4 left
6
Top 8 Front
9 front
Top

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.