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;
}

原文地址:https://www.cnblogs.com/tigerisland45/p/10404409.html

时间: 2024-11-15 21:22:07

UVA253 Cube painting【置换】的相关文章

uva253 Cube painting(UVA - 253)

题目大意 输入有三种颜色判断两个骰子是否相同 思路(借鉴) ①先用string输入那12个字符,然后for出两个骰子各自的字符串 ②这里用的算法是先找出第一个的三个面与第二个的六个面去比较,如果找到相同的面并且他们的对面相等那么继续寻找,直到三个面都能找到相对立的面 ③如果有一个面没有找到相等的面或者相等的对面之类的那么就break循环然后直接判断FALSE如果三个面都能满足上述条件,那么就是TRUE 代码 #include <bits/stdc++.h> using namespace st

Cube painting UVA 253

说说:一看到给立方体染色,开始还有点小害怕.毕竟高中数学里染色问题从来都不会简单.这道题的意思就是给立方体的六个面染色,然后判断两个染了色的立方体是否一样.其实仔细一想,立方体无非三对面,若开始确定两对面.如下面图Figure 1所示:假设1,2,6,5这四个面先确定(这只有唯一一种情况)之后,再放3,4,的时候无非两种情况.但这两种情况是不一样的,这也许就是题目所说的reflection.开始的想法是找出三对面,然后比较是否相等即可,不过实在想不出怎样来排除reflection的情况.所以只能

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. Figure 1. Since a cube has 6 faces, o

uva 253 - Cube painting(相同骰子)

习题4-4 骰子涂色(Cube painting, UVa 253) 输入两个骰子,判断二者是否等价.每个骰子用6个字母表示,如图4-7所示. 图4-7 骰子涂色 例如rbgggr和rggbgr分别表示如图4-8所示的两个骰子.二者是等价的,因为图4-8(a) 所示的骰子沿着竖直轴旋转90°之后就可以得到图4-8(b)所示的骰子. (a) (b) 图4-8 旋转前后的两个骰子 . Sample Input rbgggrrggbgr rrrbbbrrbbbr rbgrbgrrrrrg Sample

UVA 253 Cube painting(枚举 模拟)

题意: 按如图的顺序给定2个骰子的颜色(只有r.b.g三种颜色) 问2个骰子是否一模一样 如 可表示为"rbgggr" 和 "rggbgr", 第二个就是绕着Z轴顺时针旋转90度与第一个相同的骰子. 分析: 记录一个错误的做法:并不是只要两面两面互相映射, 如rbrggb 与 rgrgbb, 即使 rb 对应 rb.gb 对应 bg. rg对应rg, 但他们并不是一模一样的骰子.(可以借助真正的骰子旋转尝试一下,就是123456 和 153426, 想象一下2与5互

UVA253-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, ou

算法竞赛入门经典 第四章

[√ ] UVA1339 古老的密码 Ancient Cipher [√ ] UVA489 刽子手的游戏 Hangman Judge [√ ] UVA133 救济金发放 The Dole Queue [√ ] UVA213 信息解码 Message Decoding [√ ] UVA512 追踪电子表格中的单元格 Spreadsheet Tracking [√ ] UVA12412 师兄帮帮忙 A Typical Homework (a.k.a Shi Xiong Bang Bang Mang)

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

UVa253

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 p