We have a machine for painting cubes. It is supplied with three different colors: blue, red and green. Each face of the cube gets one of these colors. The cube's faces are numbered as in Figure 1.
Figure 1.
Since a cube has 6 faces, our machine can paint a face-numbered cube in different ways. When ignoring the face-numbers, the number of different paintings is much less, because a cube can be rotated. See example below. We denote a painted cube by a string of 6 characters, where each character is a b, r, or g. The
character (
) from the left gives the color of face i. For example, Figure 2 is a picture of rbgggr and Figure 3 corresponds to rggbgr. Notice that both cubes are painted in the same way: by rotating it around the vertical axis by 90
, the one changes into the other.
Input
The input of your program is a textfile that ends with the standard end-of-file marker. Each line is a string of 12 characters. The first 6 characters of this string are the representation of a painted cube, the remaining 6 characters give you the representation of another cube. Your program determines whether these two cubes are painted in the same way, that is, whether by any combination of rotations one can be turned into the other. (Reflections are not allowed.)
Output
The output is a file of boolean. For each line of input, output contains TRUE if the second half can be obtained from the first half by rotation as describes above, FALSE otherwise.
Sample Input
rbgggrrggbgr rrrbbbrrbbbr rbgrbgrrrrrg
Sample Output
TRUE FALSE FALSE
这道题乍看起来貌似很厉害的样子 其实真做起来了并没有想象中的那么难
我们只需要 把每个面旋转到第一个面,然后第一个面与它对应的面不动再进行四次旋转,进行判断就可以了。
也就是说 总共比较4*6-1=23次就可以了
我已开始老是输出TRUE一直很纠结是什么原因 最后发现原来是我在输入str后又立刻把它给清零了 呵呵浪费了我三个小时
#include<stdio.h> #include<math.h> #include<string.h> const int maxn=2000; char str[13],ch1[7],ch2[7],s[7]; int n[30][10]={{1,2,3,4,5,6},{1,4,2,5,3,6},{1,3,5,2,4,6},{1,5,4,3,2,6}, {2,1,4,3,6,5},{2,4,6,1,3,5},{2,3,1,6,4,5},{2,6,3,4,1,5}, {3,1,2,5,6,4},{3,5,1,6,2,4},{3,2,6,1,5,4},{3,6,5,2,1,4}};//定义其中的12种情况 以4,5,6打头的可以反过来直接用 int pan(char *ch1,char *ch2) { char t; int i,j; for(i=0; i<12; i++) { for(j=0; j<6; j++) s[j]=ch1[n[i][j]-1]; if(!strcmp(s,ch2)) return 1;//如果能够对应说明两个正方体是同一个正方体 for(j=5;j>=0;j--)//反过来直接用 s[5-j]=ch1[n[i][j]-1]; t=s[2]; s[2]=s[3]; s[3]=t; if(!strcmp(s,ch2)) return 1; } return 0; } int main() { while(scanf("%s",str)==1) { int i,j,flag; for(i=0; i<6; i++) ch1[i]=str[i];//将两个正方体对应分离 for(i=0; i<6; i++) ch2[i]=str[i+6]; flag=pan(ch1,ch2); if(flag) printf("TRUE\n"); else printf("FALSE\n"); } return 0; }