UVa 253 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.

picture21

Figure 1.

Since a cube has 6 faces, our machine can paint a face-numbered cube in tex2html_wrap_inline126 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 br, org. The tex2html_wrap_inline128 character ( tex2html_wrap_inline130 ) 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 tex2html_wrap_inline134 , the one changes into the other.

tex2html_wrap138 tex2html_wrap140

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

给定两个已经染好颜色的立方体,求第二个能否由第一个通过翻转得到

一个立方体一共有三种基本翻转方法,123456翻转后可以得到142536、421653、513462,对应于代码中的三个trans函数。

而一个立方体翻转能得到的所有立方体都可以在四次基本翻转之内产生(一个立方体向一个方向连续翻转四次后可以回到它原来的状态,由此推断的,没有证明过)

所以只要对第一个立方体进行三次for旋环(对应于三种翻转),每个for旋环中进行四次,即可穷举出所有翻转后可能得到的结果,再与后一个立方体进行比较可得答案

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<string>
 4 
 5 using namespace std;
 6 
 7 string trans_1(string x)
 8 {
 9     string ans,temp;
10     temp=x[0];
11     return ans=temp+x[3]+x[1]+x[4]+x[2]+x[5];
12 }
13 
14 string trans_2(string x)
15 {
16     string ans,temp;
17     temp=x[3];
18     return ans=temp+x[1]+x[0]+x[5]+x[4]+x[2];
19 }
20 
21 string trans_3(string x)
22 {
23     string ans,temp;
24     temp=x[4];
25     return ans=temp+x[0]+x[2]+x[3]+x[5]+x[1];
26 }
27 
28 int main()
29 {
30     char s[20];
31 
32     while(gets(s))
33     {
34         bool ans=false;
35         string a,b,temp;
36         temp=s[0];
37         a=temp+s[1]+s[2]+s[3]+s[4]+s[5];
38         temp=s[6];
39         b=temp+s[7]+s[8]+s[9]+s[10]+s[11];
40         if(a==b)
41             ans=true;
42 
43         string temp_1=a;
44         for(int i=0;(!ans)&&i<=3;i++)
45         {
46             temp_1=trans_1(temp_1);
47             if(temp_1==b)
48                 ans=true;
49             else
50             {
51                 string temp_2=temp_1;
52                 for(int j=0;(!ans)&&j<=3;j++)
53                 {
54                     temp_2=trans_2(temp_2);
55                     if(temp_2==b)
56                         ans=true;
57                     else
58                     {
59                         string temp_3=temp_2;
60                         for(int k=0;(!ans)&&k<=3;k++)
61                         {
62                             temp_3=trans_3(temp_3);
63                             if(temp_3==b)
64                                 ans=true;
65                         }
66                     }
67                 }
68             }
69         }
70 
71         if(ans)
72             printf("TRUE
");
73         else
74             printf("FALSE
");
75     }
76 
77     return 0;
78 }
[C++]
原文地址:https://www.cnblogs.com/lzj-0218/p/3506856.html