zoukankan      html  css  js  c++  java
  • 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


    #include<stdio.h>
    #include<string.h>
    
    int operation(char* base, char* temp)
    {
        int i, j, cnt;
        char sutemp[8];
        static int subtimes[][4] = {{2,3,4,5},{4,2,5,3},{3,5,2,4},{5,4,3,2}};
        sutemp[0] = temp[0];
        sutemp[5] = temp[5];
        sutemp[6] = '\0';
        for(i=0; i<4; ++i)
        {
            for(j=0,cnt=1; j<4; ++j, ++cnt)
                sutemp[cnt] = temp[subtimes[i][j]-1];
            if(strcmp(base, sutemp) == 0) return 1;
        }
        
        return 0;
    }
    
    int main()
    {
        char input[18], cub1[10], cub2[10], temp[10];
        int i, flag, j, cnt;
        int times[][6] = {{1,2,3,4,5,6},{2,1,4,3,6,5},{3,2,6,1,5,4},{4,2,1,6,5,3},{5,1,3,4,6,2},{6,2,4,3,5,1}};
        while(scanf("%s", input) != EOF)
        {
            flag = 0;
            cub1[6] = cub2[6] = '\0';
            memcpy(cub1, input, sizeof(char)*6);
            memcpy(cub2, input+6, sizeof(char)*6);
            if(strcmp(cub1, cub2) == 0) flag = 1;
            else 
            {
                for(i=0; i<6; ++i)
                {
                    for(j=cnt=0; j<6; ++j, ++cnt)
                        temp[cnt] = cub2[times[i][j]-1];
                    temp[cnt] = '\0';
                    flag = operation(cub1, temp);
                    if(flag) break;
                }            
            }
            if(flag) printf("TRUE\n");
            else printf("FALSE\n");
    
        }
        return 0;
    }

    解题思路:

    这题对于我来说,没有所谓的技巧,纯粹的遍历,看到题目的人都会有的想法只是被我实现了,理所当然的,不缺乏普遍的缺陷,麻烦冗长,无技巧,但最后我还是有所WA

    遍历24种情况,花不了多少的时间,所以这样做还是能够接受的。

  • 相关阅读:
    Leetcode题解(十六)
    Leetcode题解(十五)
    Leetcode题解(十四)
    Leetcode题解(十三)
    Leetcode题解(十二)
    Leetcode题解(十一)
    php数据库访问及增删改
    克隆对象及加载类
    抽象
    PHP继承
  • 原文地址:https://www.cnblogs.com/liaoguifa/p/2932573.html
Copyright © 2011-2022 走看看