Source: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5012
Dice
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 307 accepted submission (s): 183
Problem descriptionthere are 2 special dices on the table. on each face of the dice, a distinct number was written. consider a1.a2, A3, A4, A5, a6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice. similarly, consider b1.b2, B3, B4, B5, B6 to be numbers on specific faces of dice B. it's guaranteed that all numbers written on dices are integers no smaller than 1 and no more than 6 while AI =aj and Bi =bj for all I =j. specially, sum of numbers on opposite faces may not be 7.
At the beginning, the two dices may face different (which means there exist some I, AI = Bi ). ddy wants to make the two dices look the same from all directions ctions (which means for all I, AI = Bi) only by the following four rotation operations. (Please read the picture for more information)
Now ddy wants to calculate the minimal steps that he has to take to achieve his goal.
Inputthere are multiple test cases. Please process till EOF.
For each case, the first line consists of six integers A1, A2, A3, A4, A5, A6, representing the numbers on Dice.
The second line consists of six integers B1, B2, B3, B4, B5, B6, representing the numbers on Dice B.
Outputfor each test case, print a line with a number representing the answer. If there's no way to make two dices exactly the same, output-1.
Sample Input
1 2 3 4 5 61 2 3 4 5 61 2 3 4 5 61 2 5 6 4 31 2 3 4 5 61 4 2 5 3 6
Sample output
03-1
Source2014 ACM/ICPC Asia Regional Xi 'an online
There are two sides. You can flip 90 degrees forward, back, left, or right each time. To give you the initial state of the slave, the output will reach the minimum number of times of the two sieve in the same State. If it cannot be reached, the output will be-1.
Question: BFs, online game virgin A, two TLE ~~ I used Hash to determine the weight of AC. I don't know if I need to use this ~~ It seems that there are not many statuses, so you should be able to use common methods too! BFS is used to traverse the four situations.
AC code:
# Include <cstdio> # include <cmath> # include <cstring> # define Max 5000000 bool visit [2000000] = {0}; struct node {int A [6], step;} map [Max], temp, end; int calc [6] = {,}; int Hash (node X) {int sum = 0; for (INT I = 0; I <6; I ++) {sum + = x. A [I] * calc [I];} return sum;} int main () {// Number of the six faces; 2 under 0; left 3; right 1; first 4; last 5 while (~ Scanf ("% d", & map [0]. A [0], & map [0]. A [2], & map [0]. A [3], & map [0]. A [1], & map [0]. A [4], & map [0]. A [5]) {memset (visit, 0, sizeof (visit); scanf ("% d", & End. A [0], & End. A [2], & End. A [3], & End. A [1], & End. A [4], & End. A [5]); map [0]. step = 0; end. step = 0; visit [Hash (end)] = 1; visit [Hash (Map [0])] = 1; int exdir = 0, nodedir = 0, flag = 1; if (Hash (Map [0]) = hash (end) {// exactly the same, directly output 0 printf ("% d \ n", 0); continue ;} while (nodedir <= exdir & Exdir <Max & flag) {for (INT I = 0; I <4; I ++) {temp = map [nodedir]; // flip forward if (! I) {int X, Y; X = temp. A [0]; temp. A [0] = temp. A [5]; y = temp. A [4]; temp. A [4] = x; X = temp. A [2]; temp. A [2] = y; temp. A [5] = x;} // flip the else if (I = 1) {int x = temp. A [5]; temp. A [5] = temp. A [0]; int y = temp. A [2]; temp. A [2] = x; X = temp. A [4]; temp. A [4] = y; temp. A [0] = x;} // flip else if (I = 2) {int x = temp to the left. A [3]; temp. A [3] = temp. A [0]; int y = temp. A [2]; temp. A [2] = x; X = temp. A [1]; temp. A [1] = y; temp. A [0] = x;} // flip else {int x = temp to the right. A [1]; temp. A [1] = temp. A [0]; int y = temp. A [2]; temp. A [2] = x; X = temp. A [3]; temp. A [3] = y; temp. A [0] = x;} temp. step ++; If (Hash (temp) = hash (end) {// to reach the target, print printf ("% d \ n", temp. step); flag = 0; break;} If (! Visit [Hash (temp)]) {// Add a new node to the queue map [++ exdir] = temp; visit [Hash (temp)] = 1 ;}} nodedir ++;} If (FLAG) printf ("-1 \ n"); // unable to reach the target status} return 0 ;}
Hdoj 5012 dice -- question F in Xi'an division of the 2014 cyber Competition