码迷,mamicode.com
首页 > 其他好文 > 详细

Cube painting UVA 253

时间:2014-08-06 23:11:42      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:acm   c   uva   源代码   算法   

说说:一看到给立方体染色,开始还有点小害怕。毕竟高中数学里染色问题从来都不会简单。这道题的意思就是给立方体的六个面染色,然后判断两个染了色的立方体是否一样。其实仔细一想,立方体无非三对面,若开始确定两对面。如下面图Figure 1所示:假设1,2,6,5这四个面先确定(这只有唯一一种情况)之后,再放3,4,的时候无非两种情况。但这两种情况是不一样的,这也许就是题目所说的reflection。开始的想法是找出三对面,然后比较是否相等即可,不过实在想不出怎样来排除reflection的情况。所以只能采用第二种方法。设计一个数据结构,包含一个面的对立面的编号,以及一个数组,顺序存放从该面看过去逆时针获取其余四面的编号。最后只要将立方体一固定,尝试以立方体二的各个面为底面,然后判断对立面,以及其余四面是否能通过旋转达到完全相等即可!


题目:

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所示编号

bubuko.com,布布扣

Figure 1.

Since a cube has 6 faces, our machine can paint a face-numbered cube in bubuko.com,布布扣 different ways. When ignoring the face-numbers, the

因为一个立方体有6个面,我们的机器对编上号的立方体总共有bubuko.com,布布扣种染色方案。当忽略面的编号时,

 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个字符的字符串表示一个染了色的

 6 characters, where each character is a br, or g. The bubuko.com,布布扣 character ( bubuko.com,布布扣 ) from the left gives the color of face i. For example,

立方体,每个字符只可能是b,r或者g.从左边开始第i个字符(1<=i<=6)表示第i面。

 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

比如Figure 2就可以用rbgggr表示。Figure 3 和rggbgr相对应。注意两个立方体的着色方式其实是一样的:将其中一个沿轴

 around the vertical axis by 90 bubuko.com,布布扣 , the one changes into the other.

水平旋转90度,就变成了另外一个。

bubuko.com,布布扣bubuko.com,布布扣

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

输入是一个文本文件以EOF结束。每一行输入有12个字符。前6个字符代表着了色的立方体,剩下的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

输出是一个布尔数组成的文件。输出TRUE如果第二个着色立方体可以通过旋转变为第一个,否则输出FALSE

 rotation as describes above, FALSE otherwise.

Sample Input

rbgggrrggbgr
rrrbbbrrbbbr
rbgrbgrrrrrg

Sample Output

TRUE
FALSE
FALSE


源代码:

#include <stdio.h>

typedef struct describe{//当一个面为底面时
 int top;//该底面的对立面
 int around[4];//从该面看过去,其余四面按逆时针排列
}DES;

DES cube[6];
char c[13],c1[7],c2[7];

void init();//将各面信息初始化
void get_des();//从字符串分解各面的representation
int judge(int);//判断两立方体是否相同

int main(){
  int i,j,k,flag;
  init();

  //freopen("data.txt","r",stdin);
  while(~scanf("%s",c)){
    get_des(c,c1,c2);
    flag=0;
    for(i=0;i<6;i++)
        if(c2[i]==c1[0])//若立方体二有一面与立方体一的着色相同
            if(judge(i)){
                printf("TRUE\n");
                flag=1;
                break;
            }

    if(!flag) printf("FALSE\n");
  }
  return 0;
}

void get_des(){
 int i,j;
 for(i=0;i<6;i++)
    c1[i]=c[i];

 for(i=6,j=0;i<12;i++,j++)
    c2[j]=c[i];

}

void init(){//为方便起见,将题目中面的序号都减一
 cube[0].top=5;
 cube[0].around[0]=1,cube[0].around[1]=3,cube[0].around[2]=4,cube[0].around[3]=2;
 cube[5].top=0;
 cube[5].around[0]=1,cube[5].around[1]=2,cube[5].around[2]=4,cube[5].around[3]=3;
 cube[1].top=4;
 cube[1].around[0]=2,cube[1].around[1]=5,cube[1].around[2]=3,cube[1].around[3]=0;
 cube[4].top=1;
 cube[4].around[0]=2,cube[4].around[1]=0,cube[4].around[2]=3,cube[4].around[3]=5;
 cube[2].top=3;
 cube[2].around[0]=1,cube[2].around[1]=0,cube[2].around[2]=4,cube[2].around[3]=5;
 cube[3].top=2;
 cube[3].around[0]=1,cube[3].around[1]=5,cube[3].around[2]=4,cube[3].around[3]=0;
}

int judge(int face){
   int i,j,k;

   if(c1[cube[0].top]!=c2[cube[face].top])
    return 0;

   for(i=0;i<4;i++){//立方体一固定,立方体二旋转,判断是否存在匹配情况
    k=0;
    for(j=0;j<4;j++)
      if(c2[cube[face].around[(i+j)%4]]!=c1[cube[0].around[j]]){
        k=1;
        break;
      }
    if(!k) return 1;
   }

   return 0;
}


Cube painting UVA 253,布布扣,bubuko.com

Cube painting UVA 253

标签:acm   c   uva   源代码   算法   

原文地址:http://blog.csdn.net/u011915301/article/details/38402699

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!