UVa 253 Cube painting

来源:互联网 发布:二百人公司网络规划 编辑:程序博客网 时间:2024/04/26 00:48

这道题酝酿三天了,O(∩_∩)O哈哈~,其实就是懒~~

而且是1A哦~

算是近期做的比较难的一道题了。

本题可以参考刘汝佳的《算法竞赛入门经典训练指南》里第一章例8(Colored Cubes, LA 3401),本题只是书中例题的简化版。

问题分析:怎么判断两个正方体是否相等呢?我是用一个结构体来存放CUBE的,其中有三个变量就记录了每个正方体每种颜色的面数。

举个例子,如果有个正方体有两个面试红色,而另一个没有红色面,那这两个肯定不等。

所以,两个正方体相等的必要条件就是每种颜色的面数都相等。根据这一条件即可否定一部分情况。

 Cube painting 

We have a machine for painting cubes. It is supplied with threedifferent colors: blue, red and green. Each face of the cube gets one of thesecolors. The cube's faces are numbered as in Figure 1.

Figure 1.

Since a cube has 6 faces, our machine can paint a face-numberedcube in   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 astring of 6 characters, where each character is a br, or g. The  character (   ) from the leftgives the color of face i.For example, Figure 2 is a picture of rbgggr and Figure 3corresponds to rggbgr. Notice thatboth cubes are painted in the same way: by rotating it around the vertical axisby 90   , the onechanges into the other.

 

Input

The input of your program is a textfile that ends with thestandard end-of-file marker. Each line is a string of 12 characters. The first6 characters of this string are the representation of a painted cube, theremaining 6 characters give you the representation of another cube. Yourprogram 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, outputcontains TRUE if the secondhalf can be obtained from the first half by rotation as describes above,FALSE otherwise.

SampleInput

rbgggrrggbgr
rrrbbbrrbbbr
rbgrbgrrrrrg

SampleOutput

TRUE
FALSE
FALSE

好了,下面是核心的内容。

我们要通过计算来算出第一个正方体旋转后的每个面的颜色来看看是否和第二个正方体对应面颜色一样。

那么一个正方体有多少种摆放方式呢?

第一步,确定一个顶面;第二部确定朝前的那个面,

所以一共有6 * 4 = 24 种


用T[i]中元素表示i号颜色 由T[i]号面的颜色旋转而来。

这句话很重要,可以说是这个程序的核心思想。

比如说图中正方体从上往下看逆时针旋转90°,所得到的T[i]为{0, 2, 4, 1, 3, 5}(再次体会刚才那句)


那么如何把24个T[]都找出来,一种是手工给数组复制,但这样很费时且容易出错,不好调试。

另一种方法就是写个程序,让计算机自己全部找出来,生成一个常量表,然后放在代码里。

还是刚才那种想法,我们可以自己先把向左转和向上转对应的T找出来,分别是

{0, 2, 4, 1, 3, 5} 和 {1, 5, 2, 3, 0, 4}

然后每种姿态都可以通过这两种旋转方式组合来得到。

首先让每个面都作一次顶面,然后向左转四次。

下面是打表的代码:

#define LOCAL#include <cstdio>#include <cstring>int left[] = {0, 2, 4, 1, 3, 5};int up[] = {1, 5, 2, 3, 0, 4};//p[i]表示编号i所在位置void rot(int T[], int p[]){int q[6];memcpy(q, p, sizeof(q));for(int i = 0; i < 6; ++i)p[i] = T[q[i]];}void enumerate_permutations(){freopen("253打表.txt", "w", stdout);int p0[6] = {0, 1, 2, 3, 4, 5};printf("int dice[24][6] = {\n");for(int i = 0; i < 6; ++i){int p[6];memcpy(p, p0, sizeof(p0));switch(i){<span style="white-space:pre">//0好面本来就是顶面,不用变了case 1:<span style="white-space:pre">//1号面作为顶面rot(up, p);break;case 2:<span style="white-space:pre">//下面以此类推rot(left, p);rot(up, p);break;case 3:rot(left, p);rot(left, p);rot(left, p);rot(up, p);break;case 4:rot(left, p);rot(left, p);rot(up, p);break;case 5:rot(up, p);rot(up, p);break;}for(int j = 0; j < 4; ++j){printf("{%d, %d, %d, %d, %d, %d},\n", p[0], p[1], p[2], p[3], p[4], p[5]);rot(left, p);}}printf("};\n");}int main(void){enumerate_permutations();return 0;}

好了,有了这个表,我们就可以很方便的写AC代码了。

//#define LOCAL#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct CUBE{char color[6];int num_red;int num_green;int num_blue;}cube1, cube2;//正方体的24种姿态int pos[24][6] = {{0, 1, 2, 3, 4, 5},{0, 2, 4, 1, 3, 5},{0, 4, 3, 2, 1, 5},{0, 3, 1, 4, 2, 5},{1, 5, 2, 3, 0, 4},{2, 5, 4, 1, 0, 3},{4, 5, 3, 2, 0, 1},{3, 5, 1, 4, 0, 2},{1, 2, 0, 5, 3, 4},{2, 4, 0, 5, 1, 3},{4, 3, 0, 5, 2, 1},{3, 1, 0, 5, 4, 2},{1, 3, 5, 0, 2, 4},{2, 1, 5, 0, 4, 3},{4, 2, 5, 0, 3, 1},{3, 4, 5, 0, 1, 2},{1, 0, 3, 2, 5, 4},{2, 0, 1, 4, 5, 3},{4, 0, 2, 3, 5, 1},{3, 0, 4, 1, 5, 2},{5, 4, 2, 3, 1, 0},{5, 3, 4, 1, 2, 0},{5, 1, 3, 2, 4, 0},{5, 2, 1, 4, 3, 0},};void count(CUBE &cube);bool judge(char p[], int T[], char q[]);int main(void){#ifdef LOCALfreopen("253in.txt", "r", stdin);#endifchar s[20];while(gets(s)){int i;for(i = 0; i < 6; ++i)cube1.color[i] = s[i];for(; i < 12; ++i)cube2.color[i - 6] = s[i];count(cube1);count(cube2);bool flag = false;//两个正方体要能重合首先每种颜色的面数应当相等if(cube1.num_red != cube2.num_red|| cube1.num_green != cube2.num_green|| cube1.num_blue != cube2.num_blue){printf("FALSE\n");continue;}for(i = 0; i < 24; ++i){flag = judge(cube1.color, pos[i], cube2.color);if(flag)break;}if(flag)printf("TRUE\n");elseprintf("FALSE\n");}return 0;}//统计小正方体每种颜色的面数void count(CUBE &cube){cube.num_red = 0;cube.num_green = 0;cube.num_blue = 0;for(int i = 0; i < 6; ++i){if(cube.color[i] == 'r')++cube.num_red;if(cube.color[i] == 'g')++cube.num_green;if(cube.color[i] == 'b')++cube.num_blue;}}//判断cube1按姿态T旋转能否和cube2重合//T[i]中元素可以理解为,i号颜色 由T[i]号面的颜色旋转而来bool judge(char p[], int T[], char q[]){char p0[6];int i;for(i = 0; i < 6; ++i)p0[i] = p[T[i]];//这句应该就是整个程序最核心的代码了吧 //也很难理解 for(i = 0; i < 6; ++i)if(p0[i] != q[i])return false;return true;}



0 0
原创粉丝点击