UVa 10051-tower of Cubes

Source: Internet
Author: User

Title: Give you n cubes, each face has a color, they are arranged in a way of increasing weight, now require them to be lined up in a tower,

Each layer of a cube, requires each cube above the weight of the cube is less than he, but also to ensure that the contact surface of the same color,

Ask how many layers the highest can be placed, the answer is not unique.

Analysis: DP, dynamic programming, Lis. The maximum non-descending subsequence, which has been sequenced, satisfies the same surface color as the contact.

Defined state: F (i,k) is the maximum height of the K face up when the first block is the top;

Transfer equation: F (i,k) = Max (f (j,f)) {J < I, and the same face at the same place};

Each block is placed in 6 French, using a one-dimensional array compression state record precursor, output path.

Explanation: Go to opposite side direct different or operation can, long time did not brush the question ╮(╯▽╰)╭.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace std;int cubes[505][6];int length[505][6];int Front[3333];char face[6] [7] = {' Front ', ' Back ', ' left ', ' right ', ' top ', ' bottom '};void output (int space, int n) {if (space) {printf ("%d%s\n", N-spa CE/6,FACE[SPACE%6]); output (Front[space], n);}} int main () {int n,t = 0;while (~scanf ("%d", &n) && N) {for (int i = n; I >= 1;--i) {for (int j = 0; J < 6; + + j) {scanf ("%d", &cubes[i][j]);}} int max = 0,space = 0;for (int i = 1; I <= n; + + i) {for (int k = 0; k < 6; + + K) {length[i][k] = 1;front[6*i+k] = 0 ; for (int j = 1; j < I; + + j) {for (int f = 0; F < 6; + f) {if (cubes[j][f] = = cubes[i][k^1] && length[j][f ] >= Length[i][k]) {length[i][k] = length[j][f]+1;front[6*i+k] = 6*j+f;}}} if (Max < length[i][k]) {max = Length[i][k];space = 6*i+k;}}} if (t + +) printf ("\ n");p rintf ("Case #%d\n", T);p rintf ("%d\n ", max); output (space, n+1);} return 0;}


UVa 10051-tower of Cubes

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.