Fork me on GitHub

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种情况,花不了多少的时间,所以这样做还是能够接受的。

posted @   Gifur  阅读(579)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
TOP
点击右上角即可分享
微信分享提示