Question:
A 2x2 cube is provided, and a limited step length is given. Up to this length can be used to successfully spell Several faces.
Purely simulate the question, clarify the transformation relationships of several faces, simplify the steps, three rotation methods, and two rotation directions. Both BFS and DFS can be used.
# Include <stdio. h> # include <string. h ># define maxn 300000int twist [3] [3] [4] ={{, 7, 17,21 },{, 19,23 },{, 14,15 }}, {, 17,8}, {, 16,14}, {6, 12, 13, 7 }},{ {0, 2, 3, 1}, {23,4, 6, 8, 7,9 }},}; struct P {int State [24]; int step;} p [maxn]; int usage [24]; int head, tail, N, ans; int judge (int * A) {int ans = 0; if (A [0] = A [1] & A [0] = A [2] & A [0] = A [3]) ans ++; if (A [4] = A [5] & A [4] = A [10] & A [4] = A [11]) ans ++; if ([6] = A [7] & A [6] = A [12] & A [6] = A [13]) ans ++; if (A [8] = A [9] & A [8] = A [14] & A [8] = A [15]) ans ++; if (A [16] = A [17] & A [16] = A [18] & A [16] = A [19]) ans ++; if (A [20] = A [21] & A [20] = A [22] & A [20] = A [23]) ans ++; return ans;} int BFS () {head = 0, tail = 1; int I, j, k; int index, TT; P [0]. step = 0;/* For (k = 0; k <24; k ++) printf ("% 2D", k); printf ("\ n "); for (k = 0; k <24; k ++) printf ("% 2D", P [0]. state [k]); printf ("\ n"); */ANS = judge (P [0]. stat E); While (Head <tail) {If (P [head]. step = n | ans = 6) break; for (I = 0; I <3; I ++) {memcpy (usage, P [head]. state, sizeof (usage); For (j = 0; j <3; j ++) {for (k = 1; k <4; k ++) {Index = twist [I] [J] [k]; TT = usage [twist [I] [J] [0]; usage [twist [I] [J] [0] = usage [Index]; usage [Index] = TT ;}k = judge (usage ); ans = ans> K? Ans: K; If (P [head]. step + 1 <n) {memcpy (P [tail]. state, usage, sizeof (usage); P [tail ++]. step = P [head]. step + 1 ;}for (j = 0; j <3; j ++) {for (k = 1; k <4; k ++) {Index = twist [I] [J] [k]; TT = usage [twist [I] [J] [0]; usage [twist [I] [J] [0] = usage [Index]; usage [Index] = TT ;}for (j = 0; j <3; j ++) {for (k = 1; k <4; k ++) {Index = twist [I] [J] [k]; tt = usage [twist [I] [J] [0]; usage [twist [I] [J] [0] = usage [Index]; usage [Index] = TT ;}} K = judge (usage); ans = ans> K? Ans: K; If (P [head]. step + 1 <n) {memcpy (P [tail]. state, usage, sizeof (usage); P [tail ++]. step = P [head]. step + 1 ;}} head ++;} return ans ;}int main () {// freopen ("D: \ a.txt", "W", stdout ); int I, n; while (scanf ("% d", & N )! = EOF) {for (I = 0; I <24; I ++) scanf ("% d", P [0]. state + I); n = BFS (); printf ("% d \ n", n);} return 0 ;}
2013 Changsha field game K (pocket cube)