253 - Cube painting
Time limit: 3.000 seconds
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, org. 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个面,注意顺序。
完整代码:
#include <iostream>
using namespace std;
char str[15];
int judge(int a, int b, int c, int d, int e, int f)
{
if (str[a] == str[0] && str[b] == str[5])
{
if (str[1] == str[c] && str[3] == str[d] && str[4] == str[e] && str[2] == str[f]) return true;
if (str[1] == str[d] && str[3] == str[e] && str[4] == str[f] && str[2] == str[c]) return true;
if (str[1] == str[e] && str[3] == str[f] && str[4] == str[c] && str[2] == str[d]) return true;
if (str[1] == str[f] && str[3] == str[c] && str[4] == str[d] && str[2] == str[e]) return true;
return false;
}
return false;
}
int main()
{
while (cin >> str)
{
if (judge(6, 11, 7, 9, 10, 8)
||judge(11, 6, 10, 9, 7, 8)
||judge(7, 10, 11, 9, 6, 8)
||judge(10, 7, 6, 9, 11, 8)
||judge(8, 9, 11, 7, 6, 10)
||judge(9, 8, 6, 7, 11, 10))
cout << "TRUE" << endl;
else cout << "FALSE" << endl;
}
}

本文深入探讨了代码逻辑的解析与优化方法,通过实例分析帮助开发者理解并提升代码效率。从基本逻辑结构到高级优化策略,全面覆盖,旨在为开发者提供实用的技术指导。
442

被折叠的 条评论
为什么被折叠?



