UV 10051 tower of cubes (DP longest stereo stack)

Source: Internet
Author: User
Zookeeper

Each of the N cubes has a color. When the serial number of a cube is smaller than that of another cube and the color of the bottom of the cube is equal to the color of the top of the other cube, the cube can be placed on the other cube. the maximum height of these cubes;

Each cube can be placed in six ways. To facilitate control, the individual prefers to divide a cube into six. In this way, each cube only needs to consider the top surface and the bottom surface. d [I] indicates that the cube is decomposed and the first cube is maximum height that can be achieved on the base J from 1 to I-1 enumeration when Top [I] = bot [J] d [I] = max (d [I], d [J] + 1)



#include<cstdio>#include<cstring>using namespace std;const int N = 3005;int buf[6], d[N], pre[N], n, m, ans;char face[6][8] = {"front", "back", "left", "right", "top", "bottom"};struct Cube{    int wei, bot, top;  } cube[N];void print (int i){    if (pre[i])  print (pre[i]);    printf ("%d %s\n", cube[i].wei, face[ (i - 1) % 6]);    return;}int main(){    int cas = 0;    while (scanf ("%d", &n), n)    {        for (int i = m = 1; i <= n; ++i)        {            for (int k = 0; k < 6; ++k)                scanf ("%d", &buf[k]);            for (int k = 0; k < 6; ++k)            {                cube[m].wei = i;                cube[m].top = buf[k];                cube[m++].bot = (k % 2 ? buf[k - 1] : buf[k + 1]);            }        }        memset (d, 0, sizeof (d));        memset (pre, 0, sizeof (pre));        for (int i = ans = 1; i <= 6 * n; ++i)            for (int j = d[i] = 1; j < i; ++j)                if (cube[j].wei < cube[i].wei && cube[j].bot == cube[i].top && d[i] < d[j] + 1)                {                    d[i] = d[j] + 1;                    pre[i] = j;                    if (d[i] > d[ans])                        ans = i;                }        if (cas) printf ("\n");        printf ("Case #%d\n%d\n", ++cas, d[ans]);        print (ans);    }    return 0;}

Problem A: Tower of cubes

In this problem you are givenNColorful 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 (could t 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 in multiple test cases. The first line of each test case contains an integerN() Indicating the number of cubes you are given.I-Th () of the nextNLines contains the description ofI-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 cubeNIs the heaviest.

The input terminates with a value0ForN.

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




UV 10051 tower of cubes (DP longest stereo stack)

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.