1. 程式人生 > 其它 >Cube painting UVA - 253

Cube painting UVA - 253

技術標籤:VJ刷題練習演算法c語言

 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.

在這裡插入圖片描述

 Since a cube has 6 faces, our machine can paint a face-numbered cube in 36 =729 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 ‘b’, ‘r’, or ‘g’. The i-th character (1≤ i ≤6) 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 Figure 1 axis by 90°, the one changes into the other.

在這裡插入圖片描述

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

HINT

   暴力AC是真的爽啊!!!一開始的想法是將一個正方形分成3個環形的面,然後以一個為標準另一個正方體的面為對照,一共有3!=6種組合方式。自我感覺是沒有啥毛病的,但是從udebug上的資料發現了好幾個錯誤,修改了半天一直修不好。然後果斷放棄幻想直接暴力。

暴力方法:

  無論一個正方體怎麼旋轉6個面還是6個面,將所有的6個面的一種情況列出來,然後上底面和下底面固定時又對應四種情況再次列舉出來。直接利用迴圈判斷每一種情況,暴力解決。

Accepted

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//6個面分別位於上底面時的其中一種情況
int arr[6][6] = { {1,2,3,4,5,6},{2,6,3,4,1,5},{3,2,6,1,5,4},{4,2,1,6,5,3},{5,1,3,4,6,2},{6,5,3,4,2,1} };
//上底面和下地面固定對應的4種情況
int arr1[4][6] = { {1,2,3,4,5,6},{1,3,5,2,4,6},{1,4,2,5,3,6},{1,5,4,3,2,6} };

void translate(char* temp, char* s, int* arr2)
{
	for (int i = 0;i < 6;i++)
		temp[i] = s[arr2[i] - 1];
}

int main()
{
	char s[13];
	while (scanf("%s", s) != EOF)
	{
		char s1[7] = { 0 };
		char s2[7] = { 0 };
		strncpy(s1, s, 6);	//將兩個立方體區分開來
		strncpy(s2, s+6, 6);
		int flag = 0;
		for (int i = 0;i < 6;i++)
		{
			char temp[7] = { 0 };
			char temp1[7] = { 0 };
			translate(temp, s1, arr[i]);//先找到一種情況
			for (int j = 0;j < 4;j++)
			{
				translate(temp1, temp, arr1[j]);//判斷著一種情況對應的4個狀態
				if (strncmp(temp1, s2, 6) == 0)
				{
					flag = 1;
					break;
				}
			}
			if (flag)break;
		}
		printf("%s\n", flag == 1 ? "TRUE" : "FALSE");
	}
}
			}
		}
		if (flag)break;
	}
	printf("%s\n", flag == 1 ? "TRUE" : "FALSE");
}

}