// Tower of cubes (cube Tower) // PC/Ultraviolet IDs: 110906/10051, popularity: C, success rate: high level: 3 // verdict: accepted // submission date: 2011-10-01 // UV Run Time: 0.168 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net /// [Problem description] // give you n color cubes of different quality. Each cube is not a single color-in fact, each side is painted with a different // color. Your job is to use these cubes to build the highest tower so that (1) the cube above each cube is lighter than it (2) // The Color of the bottom surface of each cube (except the cube at the bottom of the tower) must be the same as the top color of the cube under it. //// [Input] // The input contains several groups of data. The first behavior of each group of data is an integer N (1 <=n <= 500), indicating the number of cubes. In the next n rows of //, row I describes the cube I. The six numbers in this description indicate the colors of the front, back, left, right, top, and/of the cube. The colors are 1 ~ An integer between 100. Assume that the input cube quality increases progressively, that is, 1st are // lightest and N is heaviest. /// When n = 0, the input ends. //// [Output] // for each group of data, the sequence number of the output data in a separate row is shown in the sample format, and the maximum height of the Output tower in the next row is displayed. Next, describe each layer of the tower in the order of // from top to bottom. Describe a cube in each line: first give the input sequence number, then a space and a character string (front, back, left, right, top, or bottom) indicating the coloring method ), represent the top surface of the cube in the tower. // It is the surface in the input table. If there are multiple solutions, any one can be output. //// Output of two adjacent groups of data are separated by an empty row. /// [Sample input] // 3 // 1 2 2 2 1 2 // 3 3 3 3 3 // 3 2 1 1 1 1 // 10/ /1 5 10 3 6 5 // 2 6 7 3 6 9 // 5 7 3 2 1 9 // 1 3 3 5 8 10 // 6 6 2 2 4 4 4/ /1 2 3 4 5 6 // 10 9 8 7 6 5 // 6 1 2 3 4 7 // 1 2 3 2 1 // 3 2 2 2 1 2 3/ /0 // [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 // 10 top /// [solution] // each one side of the cube and its corresponding side constitute an edge, cubes constitute some sides, and then you need Find the longest path between cubes. This is the problem. The first test data can be converted for better understanding. /// 2 1 2 2 1 // | (cube 1) // 1 2 2 2 1 2 // 3 3 3 3 3 3 3 // | (cube 2) // 3 3 3 3 3 3 3 // | // 2 3 1 1 1 1 // | (cube 3) // 3 2 1 1 1 1 //// it can be modeled as a directed acyclic graph on the top, and the longest path is the desired path. Due to the characteristics of this question, it is similar to that of the WordPress 10029. You can also use the longest increasing subsequence algorithm to solve the problem, or directly use dynamic planning to solve the problem, this makes it easier. For ease of use, use dynamic planning. Since the total number of colors for a cube is a maximum of 100, you can create an array tower. The element Tower [I] indicates the maximum height of the tower whose base color is I. The front and back correspond to the corresponding faces, such as left and right, top, and bottom, if the height of the tower whose base color is I can be increased, the height of the tower whose base color is I will be updated, and the top color of the tower will be recorded to facilitate the // final output. # Include <iostream >#include <cstring> using namespace STD; # define maxcolors 100 # define faces 6 # define maxn 500int Tower [maxcolors + 1]; // The base color is the maximum height of the tower I can constitute. Int temp [maxcolors + 1]; // save temporarily to avoid direct operations on the tower array and to avoid damage to the results. Int path [maxcolors + 1] [maxn + 1]; // The base color is the top color of the largest height tower consisting of I. Int backup [maxcolors + 1] [maxn + 1]; // temporary operation to avoid damaging the results of the array path. Int main (int ac, char * AV []) {int N, cases = 1, top, bottom; string faces [faces] = {"Front", "back ", "Left", "right", "TOP", "bottom"}; while (CIN> N, N) {// The two adjacent outputs are separated by spaces. If (cases> 1) cout <Endl; // reset the maximum tower height of each color, that is, the corresponding path. Memset (tower, 0, sizeof (Tower); memset (path, 0, sizeof (PATH); // The maximum tower height is initially 0. Int highest = 0; // The bottom color of the maximum height can be obtained. Int color = 0; // when processing, the color of the two faces is read at a time as the color of the top and bottom. For (INT I = 1; I <= N; I ++) {// operate on the copy of the current result. Memcpy (temp, Tower, sizeof (Tower); memcpy (backup, path, sizeof (PATH); For (Int J = 1; j <= (faces/2 ); j ++) {CIN> top> bottom; // If the height of the bottom tower is smaller than the maximum height found at present, copy the path to the copy. If (Tower [Top] + 1)> temp [bottom]) {memcpy (Backup [bottom], path [Top], sizeof (path [Top]); backup [bottom] [I] = 2 * j-1; temp [bottom] = Tower [Top] + 1; if (temp [bottom]> highest) {highest = temp [bottom]; color = bottom ;}/// if the height of the top-colored tower is smaller than the current maximum height //, replace it, copy the path to the copy. If (Tower [bottom] + 1)> temp [Top]) {memcpy (Backup [Top], path [bottom], sizeof (path [bottom]); backup [Top] [I] = 2 * j; temp [Top] = Tower [bottom] + 1; if (temp [Top]> highest) {highest = temp [Top]; color = Top ;}}// restore the data on the copy. Memcpy (tower, temp, sizeof (temp); memcpy (path, backup, sizeof (Backup);} // output the result. Cout <"case #" <cases ++ <Endl; cout