Problem A: Tower of Cubes
In this problem you are given
N
colorful 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:
Problem A: Tower of Cubes |
- Never put a heavier cube on a lighter one.
- The bottom face of every cube (except 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 multiple test cases. The first line of each test case contains an integer N (![$1 \le N \le 500$](http://uva.onlinejudge.org/external/100/10051img2.gif)
![$1 \le i \leN$](http://uva.onlinejudge.org/external/100/10051img3.gif)
The input terminates with a value 0 for N.
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 whitespace 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
3 1 2 2 2 1 2 3 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 1 2 3 4 5 6 10 9 8 7 6 5 6 1 2 3 4 7 1 2 3 3 2 1 3 2 1 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
Miguel Revilla
2000-12-28
一道水dp搞了这么久,初始化写错了,应该每个都初始化成1,而不是只把第一个的初始化成1.最后实在没办法了,生成了点数据对拍才发现问题。
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <iostream>
#include <stack>
#include <set>
#include <cstring>
#include <stdlib.h>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 1000000000;
const int maxn = 500 + 5;
const int maxc = 6;
char s[6][10] = {"front", "back", "left", "right", "top", "bottom"};
int dp[maxn*maxc];
int trace[maxn*maxc];
int n;
vector<int> ans;
P cube[maxn*maxc];
void get_ans(int x){
ans.clear();
int pos = x;
ans.push_back(x);
while(trace[pos] != -1){
ans.push_back(trace[pos]);
pos = trace[pos];
}
reverse(ans.begin(), ans.end());
}
int change(int order, int pos){
return order + pos*n;
}
void solve(){
memset(dp, 0, sizeof(dp));
//for(int i = 0;i < maxc;i++) dp[i*n] = 1;
memset(trace, -1, sizeof(trace));
for(int i = 0;i < n;i++){
for(int j = 0;j < i;j++){
for(int x = 0;x < maxc;x++){
for(int y = 0;y < maxc;y++){
int to = change(i, x);
int from = change(j, y);
if(cube[to].first == cube[from].second){
if(dp[to] < dp[from]+1){
dp[to] = dp[from]+1;
trace[to] = from;
}
}
}
}
}
}
int ansn = 0, target = 0;
for(int i = 0;i < maxn*maxc;i++){
if(dp[i] > ansn){
ansn = dp[i];
target = i;
}
}
//cout << ansn << ' ' << target%n+1 << ' ' << target/n << endl;
get_ans(target);
}
int main(){
//freopen("input.txt", "r", stdin);
//freopen("output2.txt", "w", stdout);
int kase = 0;
while(scanf("%d", &n)){
if(n == 0) break;
for(int i = 0;i < n;i++){
for(int j = 0;j < maxc;j+=2){
int color1, color2;
scanf("%d%d", &color1, &color2);
cube[change(i, j)] = P(color1, color2);
cube[change(i, j+1)] = P(color2, color1);
}
}
solve();
if(kase != 0)
printf("\n");
kase++;
printf("Case #%d\n", kase);
printf("%d\n", ans.size());
for(int i = 0;i < ans.size();i++){
printf("%d %s\n", ans[i]%n+1, s[ans[i]/n]);
}
}
return 0;
}
/*
5
12 83 41 90 74 82
27 79 1 38 57 49
39 50 17 53 5 36
34 66 78 47 9 10
33 18 12 96 52 65
3
94 5 92 76 67 90
52 35 78 60 18 22
35 1 47 63 33 69
8
49 73 42 24 64 40
12 15 27 60 34 2
58 88 77 12 60 81
57 66 10 35 45 80
73 61 46 67 100 73
54 53 78 15 6 34
75 13 66 20 24 92
30 28 13 72 62 27
3
30 42 26 97 9 5
53 12 13 39 32 73
84 64 23 33 49 38
3
18 59 10 46 34 7
54 92 3 42 45 21
20 91 58 44 91 15
8
100 24 91 11 61 86
83 88 35 8 46 42
51 80 1 97 8 59
27 64 99 27 71 91
90 34 74 24 0 12
91 22 63 29 30 38
52 74 58 89 70 94
39 70 58 17 83 89
9
22 68 24 30 67 17
86 89 16 8 11 43
17 54 3 70 77 90
79 19 77 53 92 54
69 5 93 90 97 41
46 0 55 62 86 75
41 32 20 57 26 25
28 21 96 51 74 84
71 57 9 12 69 25
5
61 10 7 51 55 78
70 67 50 71 20 6
18 29 76 70 91 64
79 62 67 74 4 27
75 32 100 92 31 29
8
41 36 25 15 72 3
95 14 92 74 51 70
45 58 12 82 55 83
21 3 8 4 84 49
23 43 97 2 46 72
29 3 54 55 41 55
74 17 32 36 98 91
14 22 82 3 96 79
5
88 49 29 7 82 91
0 20 85 14 65 11
64 22 49 27 91 53
81 29 12 88 48 91
74 34 34 39 37 53
0
*/
/*
Case #1
2
1 back
5 left
Case #2
2
2 front
3 front
Case #3
2
1 front
5 front
Case #4
1
1 front
Case #5
1
1 front
Case #6
3
1 front
5 right
7 back
Case #7
3
1 top
3 front
4 bottom
Case #8
2
2 front
4 left
Case #9
2
2 left
7 front
Case #10
3
1 top
3 top
5 bottom
*/