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 (![$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
题意:让n个立方体堆积,重量大的在下面(题目按从轻到重的顺序给出立方体),然后相邻的两个面应该有相同的颜色,问如何堆积得到最大的高度。
思路:由一个立方体,枚举其六个面,然后枚举它下面可以相邻的最高的立方体的面,思路很简单,就是不太好写,有点麻烦。
AC代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{ int col[10],h[10],point[10][2],num;
}box[510];
int main()
{ int t=0,n,i,j,k,a,b,p,q,pos,ans[3],ret,pos3,f[510][2],x,y,x1,y1;
while(~scanf("%d",&n) && n)
{ ans[0]=1;ans[1]=1;ans[2]=1;
for(i=1;i<=n;i++)
for(j=1;j<=6;j++)
{ scanf("%d",&box[i].col[j]);
box[i].h[j]=1;
}
for(i=n-1;i>=1;i--)
for(p=1;p<=6;p++)
{ if(p&1)
k=box[i].col[p+1];
else
k=box[i].col[p-1];
ret=0;
for(j=i+1;j<=n;j++)
for(q=1;q<=6;q++)
if(box[j].col[q]==k)
{ if(box[j].h[q]>ret)
{ ret=box[j].h[q];
box[i].point[p][0]=j;
box[i].point[p][1]=q;
}
}
box[i].h[p]+=ret;
if(box[i].h[p]>ans[0])
{ ans[0]=box[i].h[p];
ans[1]=i;
ans[2]=p;
}
}
x=ans[1];y=ans[2];
for(i=1;i<=ans[0];i++)
{ f[i][0]=x;
f[i][1]=y;
x1=box[x].point[y][0];
y1=box[x].point[y][1];
x=x1;y=y1;
}
if(t!=0)
printf("\n");
printf("Case #%d\n",++t);
printf("%d\n",ans[0]);
for(i=1;i<=ans[0];i++)
{ printf("%d ",f[i][0]);
if(f[i][1]==1)
printf("front\n");
else if(f[i][1]==2)
printf("back\n");
else if(f[i][1]==3)
printf("left\n");
else if(f[i][1]==4)
printf("right\n");
else if(f[i][1]==5)
printf("top\n");
else if(f[i][1]==6)
printf("bottom\n");
}
}
}