骰子涂色(Cube painting)

来源:互联网 发布:淘宝店铺收藏怎么做 编辑:程序博客网 时间:2024/06/02 13:40
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, or g. 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

 

rbgggrrggbgrrrrbbbrrbbbrrbgrbgrrrrrg

 

Sample Output

 

TRUEFALSEFALSE

【题目】

       输入两个骰子,判断二者是否等价。

【分析】

       思路源于 点击打开链接 和 点击打开链接。

      1、分析可知1-6,2-5,3-4(指面对面的序号对),所以两个骰子若相同的话,必定面对面的花色是一样的,所以只要判断两个骰子所形成的三个对面是否都能够对应起来就可以了。

以下是我用java语言编写的程序代码(当然也可以查看原文的C语言代码):

import java.util.Scanner;//分析可知1-6,2-5,3-4,所以两个骰子若相同的话,必定面对面的花色是一样的,//所以只要判断两个骰子的所形成的三个对面是否都能够对应起来就可以了// http://www.cnblogs.com/zsboy/archive/2012/08/27/2659066.htmlpublic class CubePainting1 {public static void main(String[] args) {Scanner input = new Scanner(System.in);int[][] c1 = new int[300][300];int[][] c2 = new int[300][300];while(input.hasNext()) {String s = input.next();for(int i = 0; i < 300; i++)for(int j = 0; j < 300; j++) {c1[i][j] = 0;}for(int i = 0; i <= 2; i++) {c1[s.charAt(i)][s.charAt(5 - i)]++;c1[s.charAt(5 - i)][s.charAt(i)]++;}for(int i = 6; i <= 8; i++) {c2[s.charAt(i)][s.charAt(17 - i)]++;c2[s.charAt(17 - i)][s.charAt(i)]++;}boolean result = true;for(int i = 0; i <= 2; i++) {if(c1[s.charAt(i)][s.charAt(5 - i)] != c2[s.charAt(i)][s.charAt(5 - i)]) {result = false;break;}}if(result)System.out.println("TRUE");elseSystem.out.println("FALSE");}}}

       2、通过类似枚举的方法,对一个骰子进行翻转旋转,当旋转到至少有一中情况与另外一个骰子的状态相同时,即我们可以判定两者是等价的。可以首先固定某两个对面所形成的轴为中心轴,再对骰子进行顺时针旋转,每次旋转出来的结果均与另个不做任何操作的骰子进行对比。

以下是我用java语言编写的程序代码(当然也可以查看原文的C语言代码,写法略有不同思路则大体相同):

import java.util.Arrays;import java.util.Scanner;//类似枚举//通过方法内参数位置配对时的改变来模拟骰子的旋转// http://blog.csdn.net/frankiller/article/details/7671606public class CubePainting2 {public static void main(String[] args) {// TODO Auto-generated method stubScanner input = new Scanner(System.in);String s;char[] c1;char[] c2;int sum;while(input.hasNext()) {s = input.next();c1 = s.substring(0, 6).toCharArray();c2 = s.substring(6, 12).toCharArray();sum = 0;//cube1:以上下为轴sum += rotate(c1[0], c1[5], c1[1], c1[2], c1[3], c1[4], c2);sum += rotate(c1[5], c1[0], c1[1], c1[3], c1[2], c1[4], c2);//cube1:以前后为轴sum += rotate(c1[1], c1[4], c1[0], c1[3], c1[2], c1[5], c2);sum += rotate(c1[4], c1[1], c1[0], c1[2], c1[3], c1[5], c2);//cube1:以左右为轴sum += rotate(c1[2], c1[3], c1[0], c1[1], c1[4], c1[5], c2);sum += rotate(c1[3], c1[2], c1[0], c1[4], c1[1], c1[5], c2);if(sum > 0)System.out.println("TRUE");elseSystem.out.println("FALSE");}}//以up和down所形成的轴进行顺时针旋转public static int rotate(char up, char down, char a, char b, char c, char d, char[] c2) {if(up == c2[0] && down == c2[5] && a == c2[1] && b == c2[2] && c == c2[3] && d == c2[4])return 1;if(up == c2[0] && down == c2[5] && c == c2[1] && a == c2[2] && d == c2[3] && b == c2[4])return 1;if(up == c2[0] && down == c2[5] && d == c2[1] && c == c2[2] && b == c2[3] && a == c2[4])return 1;if(up == c2[0] && down == c2[5] && b == c2[1] && d == c2[2] && a == c2[3] && c == c2[4])return 1;return 0;}}


0 0