UVA253 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.
    Since a cube has 6 faces, our machine can paint a face-numbered cube in 3^6 = 729 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 i-th character (1 ≤ i ≤ 6) 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

问题链接UVA253 Cube painting
问题简述
    骰子每个面由红(red),蓝(blue),绿(green)三种颜色之一染色,骰子的每一个面按照图示方式编号。给出2个骰子的的颜色顺序,计算这一个骰子能否通过旋转得到另外一个骰子,即颜色位置一致?
问题分析
    这实际上是一个置换问题,需要暴力把各种转法全部考虑到。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA253 Cube painting */

#include <bits/stdc++.h>

using namespace std;

const int d[][6] = {
    {0, 1, 2, 3, 4, 5},
    {1, 5, 2, 3, 0, 4},
    {2, 1, 5, 0, 4, 3},
    {3, 1, 0, 5, 4, 2},
    {4, 0, 2, 3, 5, 1},
    {5, 4, 2, 3, 1, 0}
};
const int N = 6;
char s[N + N + 1], s1[N + 1], s2[N + 1], s3[N + 1];

int main()
{
    while(~scanf("%s", s)) {
        strncpy(s1, s, N);
        strncpy(s2, s + N, N);
        int flag = 0;
        for(int i = 0; i < N; i++) {        // 考虑骰子各个数字在上的情况:6种
            for(int j = 0; j < N; j++)
                s3[j] = s1[d[i][j]];
            s3[N] = '\0';
            if(strcmp(s3, s2) == 0) {
                flag = 1;
                break;
            }
            for(int j = 0; j < N / 2; j++) { // 一面朝上,只需要考虑3种转法
                char c = s3[1];
                s3[1] = s3[2];
                s3[2] = s3[4];
                s3[4] = s3[3];
                s3[3] = c;
                if(strcmp(s3, s2) == 0) {
                    flag = 1;
                    break;
                }
            }
            if(flag) break;
        }

        printf("%s\n", flag ? "TRUE" : "FALSE");
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/10404409.html