i tried to solve this problem using dynamic programming (lis)...i am not very sure if my algo is correct or if it is even considered dp, but it works as follows:
i have a 2d array tower(i, j) which is set to true or false, depending on whether i can form a tower of height i, with the color j facing up...
then for every kth cube read from the file, i look for the tallest possible height h that the cube k can be added on, if i can add it on, i will set tower(h+1, color of cube k's upward face) to true,also, i add the cube k, and its upward face to another similar 2D array so that i can print the result l8r...
after i finish going through all the cubes, i look for the highest height that can be reached and print the sequence of cubes...
i tried my program with a several test case that i can think of and the sample input, it produces the correct output....since the judge gives me the WA, i must have made soime mistakes somewhere...below is my c code....hope somebdy can provide some test cases for me to spot my mistakes or point out the problem with my algo....thx for any help
[c]
/* @BEGIN_OF_SOURCE_CODE */
#include <stdio.h>
#include <string.h>
/* @JUDGE_ID: XXXXX 10051 C "" */
#define MAX_CUBES 512
#define CUBE_FACES 6
#define MAX_COLORS 128
int opposite_face(int);
int opposite_color(int, int);
int print_seq(int[MAX_CUBES][MAX_CUBES], int, int);
char cubes[MAX_CUBES][CUBE_FACES];
char face_str[CUBE_FACES][7] = {"front", "back", "left", "right", "top", "bottom"};
int main(void)
{
int n, case_count = 0;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
while (scanf("%d\n", &n) == 1)
{
int i, j, k;
char found;
char tower[MAX_CUBES][MAX_COLORS];
int sequence[MAX_CUBES][MAX_CUBES];
if (n == 0)
break;
case_count++;
printf("Case #%d\n", case_count);
memset(cubes, 0, sizeof(cubes));
memset(tower, 0, sizeof(tower));
memset(sequence, 0, sizeof(sequence));
for (i=0;i<n;i++)
{
for (j=0;j<CUBE_FACES;j++)
scanf("%d ", (int *)&cubes
[j]);
scanf("\n");
}
for (i=0;i<n;i++)
{
0&&printf("trying cube %d...\n", i);
found = 0;
for (j=i-1;j>=0;j--)
{
0&&printf(" taking a look at length %d:\n", j);
for(k=0;k<CUBE_FACES;k++)
{
if (tower[j][(int)cubes[k]] == 1)
{
0&&printf(" found matching color: %d\n", (int)cubes[k]);
tower[j + 1][opposite_color(i, k)] = 1;
sequence[j + 1][opposite_color(i, k)] = i * 1024 + opposite_face(k);
found = 1;
}
}
if (found == 1)
break;
}
if (found == 0)
{
0&&printf(" none found, register as length 0\n");
for(k=0;k<CUBE_FACES;k++)
{
if (tower[0][opposite_color(i, k)] == 0)
{
tower[0][opposite_color(i, k)] = 1;
sequence[0][opposite_color(i, k)] = i * 1024 + opposite_face(k);
}
}
}
}
found = 0;
for (i=n;i>=0;i--)
{
for (j=0;j<MAX_COLORS;j++)
if (tower[j] == 1)
{
print_seq(sequence, i, j);
found = 1;
break;
}
if (found == 1)
break;
}
printf("\n");
}
return 0;
}
int print_seq(int seq[MAX_CUBES][MAX_CUBES], int len, int color)
{
int prev_face, prev_color, prev_cube;
int i, j;
int temp[MAX_CUBES] = {0};
j = 0;
prev_color = color;
prev_cube = seq[len][prev_color] / 1024;
prev_face = seq[len][prev_color] % 1024;
temp[j++] = seq[len][prev_color];
printf("%d\n", len + 1);
0&&printf("len %d cube %d face %d color %d\n", len, prev_cube, prev_face, prev_color);
for (i=len-1;i>=0;i--)
{
prev_color = opposite_color(prev_cube, prev_face);
prev_cube = seq[prev_color] / 1024;
prev_face = seq[prev_color] % 1024;
temp[j++] = seq[prev_color];
}
for (i=j-1;i>=0;i--)
{
prev_cube = temp / 1024;
prev_face = temp % 1024;
printf("%d %s\n", prev_cube + 1, face_str[prev_face], cubes[prev_cube][prev_face], cubes[prev_cube][opposite_face(prev_face)]);
}
return 0;
}
int opposite_face(int face)
{
switch (face)
{
case 0: return 1;
case 1: return 0;
case 2: return 3;
case 3: return 2;
case 4: return 5;
case 5: return 4;
}
return -1;
}
int opposite_color(int cube, int face)
{
return cubes[cube][opposite_face(face)];
}
/* @END_OF_SOURCE_CODE */
[/c]