骰子涂色(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, 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

 

rbgggrrggbgr
rrrbbbrrbbbr
rbgrbgrrrrrg

 

Sample Output

 

TRUE
FALSE
FALSE

【题目】

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

【分析】

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

      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.html
public 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");
			else
				System.out.println("FALSE");
		}
	}
}

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

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

import java.util.Arrays;
import java.util.Scanner;
//类似枚举
//通过方法内参数位置配对时的改变来模拟骰子的旋转
// http://blog.csdn.net/frankiller/article/details/7671606
public class CubePainting2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner 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");
			else
				System.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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值