/*
DP:有点类似DAG上的最短路径
立方体添加顺序为top->bottom
d[i][j] 表示前i个立方体组成的以颜色j为bottom的最大高度
d[i][j] = max{ d[i-1][j], d[i-1][k]+1(若第i个立方体可以组成top颜色=k, bottom颜色=j) }
时间复杂度为 O(N*M) N:为立方体个数, M为颜色个数
*/
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 502;
const int MAXM = 101;
int A[MAXN][6];
int d[MAXN][MAXM];
int s[MAXN][MAXM];
int n;
int m, idx;
char dir[6][10] = {"front", "back", "left", "right", "top", "bottom"};
void dp(){
memset(d, 0, sizeof(d));
memset(s, -1, sizeof(s));
for(int i=0; i<6; i++) {
int o=A[0][i];
d[0][o] = 1;
s[0][o] = i%2 ? i-1 : i+1;
}
for(int i=1; i<n; i++) {
for(int j=1; j<=MAXM; j++) {
d[i][j] = d[i-1][j];
}
for(int j=0; j<3; j++) {
int top = A[i][j*2], bottom=A[i][j*2+1];
if(d[i][bottom] < d[i-1][top] + 1) {
d[i][bottom] = d[i-1][top] + 1;
s[i][bottom] = j*2;
}
if(d[i][top] < d[i-1][bottom] + 1) {
d[i][top] = d[i-1][bottom] + 1;
s[i][top] = j*2+1;
}
}
}
m = d[n-1][0];
idx = 0;
for(int i=1; i<=MAXM; i++) {
if(d[n-1][i] > m) {
m = d[n-1][i];
idx = i;
}
}
}
void print(int i, int j)
{
if(i==-1) return;
int k = s[i][j];
if(k == -1) print(i-1, j);
else {
print(i-1, A[i][k]);
printf("%d %s\n", i+1, dir[k]);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int T = 0;
while(scanf("%d", &n) == 1 && n != 0) {
for(int i=0; i<n; i++) {
for(int j=0; j<6; j++) {
scanf("%d", &A[i][j]);
}
}
dp();
if(T) printf("\n");
printf("Case #%d\n%d\n", ++T, m);
print(n-1, idx);
}
return 0;
}
UVa 10051 - Tower of Cubes
最新推荐文章于 2015-01-25 15:54:58 发布