Cube painting
Cube painting |
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
TRUEFALSE
给你一个六面标有字符的正方体,再给你一定顺序的六个字符,问正方体能否通过前后左右90°翻滚变为给的字符顺序,能输出TRUE,否则输出FALSE 每次可以前后左右四个方向翻滚,可以看做bfs中的四个选择,标记下每次翻滚后字符的顺序,写的有点啰嗦了,还好一遍水过了。。。
#include <iostream> #include <cstring> #include <cstdio> using namespace std; struct node { char s1,s2,s3,s4,s5,s6; } q[100001]; int b[20][20][20][20][20][20]; void bfs(char a[]) { memset(b,0,sizeof(b)); node r,t; t.s1=a[0]; t.s2=a[1]; t.s3=a[2]; t.s4=a[3]; t.s5=a[4]; t.s6=a[5]; int k=0,l=0; q[l++]=t; while(k<l) { t=q[k++]; if(t.s1==a[6] && t.s2==a[7] && t.s3==a[8] && t.s4==a[9] && t.s5==a[10] && t.s6==a[11]) { cout<<"TRUE"<<endl; return ; } for(int i=0; i<4; i++) { if(!i) { r.s1=t.s2; r.s2=t.s6; r.s3=t.s3; r.s4=t.s4; r.s5=t.s1; r.s6=t.s5; } else if(i==1) { r.s1=t.s5; r.s2=t.s1; r.s3=t.s3; r.s4=t.s4; r.s5=t.s6; r.s6=t.s2; } else if(i==2) { r.s1=t.s3; r.s2=t.s2; r.s3=t.s6; r.s4=t.s1; r.s5=t.s5; r.s6=t.s4; } else { r.s1=t.s4; r.s2=t.s2; r.s3=t.s1; r.s4=t.s6; r.s5=t.s5; r.s6=t.s3; } int c1,c2,c3,c4,c5,c6; if(r.s1=='b') c1=1; else if(r.s1=='g') c1=2; else if(r.s1=='r') c1=3; if(r.s2=='b') c2=1; else if(r.s2=='g') c2=2; else if(r.s2=='r') c2=3; if(r.s3=='b') c3=1; else if(r.s3=='g') c3=2; else if(r.s3=='r') c3=3; if(r.s4=='b') c4=1; else if(r.s4=='g') c4=2; else if(r.s4=='r') c4=3; if(r.s5=='b') c5=1; else if(r.s5=='g') c5=2; else if(r.s5=='r') c5=3; if(r.s6=='b') c6=1; else if(r.s6=='g') c6=2; else if(r.s6=='r') c6=3; if(!b[c1][c2][c3][c4][c5][c6]) { b[c1][c2][c3][c4][c5][c6]=1; q[l++]=r; } } } cout<<"FALSE"<<endl; } int main() { char a[12]; while(cin>>a) { bfs(a); } return 0; }