2014-08-02 19:37:06
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:
- 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 ( ) indicating the number of cubes you are given. The ith (
) of the next N lines contains the description of the ith 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 cube N is the heaviest.
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
思路:又是一个经典的方块叠塔,但是这里要记录一下路径(用next[]数组),由于方块可以旋转(每个面有各自颜色),所以把一个方块变成6个(6种面朝上),但是要注意这和以前每个方块无限量有区别,每个方块只有一个,所以应该给每个方块定义一个权,使一个方块变出的6个方块有相同的权,以免对一个方块重复取用(这题恰好用重量来定义这个权)
1 /************************************************************************* 2 > File Name: k.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Thu 31 Jul 2014 03:41:44 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <iostream> 14 #include <algorithm> 15 using namespace std; 16 17 struct node{ 18 int top; 19 int bot; 20 int w; 21 int cnt; 22 vector<int> s; 23 }no[3005]; 24 25 int n,cnt; 26 int dp[3005]; 27 int next[3005]; 28 29 void Build_graph(){ 30 for(int i = 0; i < cnt; ++i) 31 for(int j = i + 1; j < cnt; ++j) 32 if(no[i].w < no[j].w && no[i].bot == no[j].top){ 33 ++no[i].cnt; 34 no[i].s.push_back(j); 35 } 36 } 37 38 int Solve(int p){ 39 if(dp[p] != -1) 40 return dp[p]; 41 int tmax = 0; 42 int tem; 43 for(int i = 0; i < no[p].cnt; ++i){ 44 if((tem = Solve(no[p].s[i])) > tmax){ 45 tmax = tem; 46 next[p] = no[p].s[i]; 47 } 48 } 49 return dp[p] = tmax + 1; 50 } 51 52 int main(){ 53 //freopen("in","r",stdin); 54 int Case = 0; 55 int co[6]; 56 char str[6][10] = {"front","back","left","right","top","bottom"}; 57 while(scanf("%d",&n) == 1 && n){ 58 memset(dp,-1,sizeof(dp)); 59 memset(next,-1,sizeof(next)); 60 memset(no,0,sizeof(no)); 61 cnt = 6 * n; 62 for(int i = 0; i < n; ++i){ 63 scanf("%d%d%d%d%d%d",&co[0],&co[1],&co[2],&co[3],&co[4],&co[5]); 64 int k = i * 6; 65 no[k].top = co[0]; 66 no[k].bot = co[1]; 67 no[k].w = i; // front 68 no[k + 1].top = co[1]; 69 no[k + 1].bot = co[0]; 70 no[k + 1].w = i; //back 71 72 no[k + 2].top = co[2]; 73 no[k + 2].bot = co[3]; 74 no[k + 2].w = i; //left 75 no[k + 3].top = co[3]; 76 no[k + 3].bot = co[2]; 77 no[k + 3].w = i; //right 78 79 no[k + 4].top = co[4]; 80 no[k + 4].bot = co[5]; 81 no[k + 4].w = i; //top 82 no[k + 5].top = co[5]; 83 no[k + 5].bot = co[4]; 84 no[k + 5].w = i; //bottom 85 } 86 Build_graph(); 87 if(Case) 88 printf("\n"); 89 printf("Case #%d\n",++Case); 90 int ans = 0,tem,st; 91 for(int i = 0; i < cnt; ++i){ 92 if((tem = Solve(i)) > ans){ 93 ans = tem; 94 st = i; 95 } 96 } 97 //for(int i = 0; i < cnt; ++i){ 98 // printf("dp[%d] : %d\n",i,dp[i]); 99 //} 100 //for(int i = 0; i < cnt; ++i){ 101 // printf("next[%d] : %d\n",i,next[i]); 102 //} 103 printf("%d\n",ans); 104 printf("%d %s\n",(st + 6) / 6,str[(st) % 6]); 105 while(next[st] != -1){ 106 printf("%d %s\n",(next[st] + 6) / 6,str[(next[st]) % 6]); 107 st = next[st]; 108 } 109 } 110 return 0; 111 }