Link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 456 & page = show_problem & problem = 4098
The example in Liu rujia's book (Training Guide P6.) is very good, especially the method for generating the rotation sequence, which is also the focus of this question. I learned.
# Include <cstdio> # include <cstring> # include <map> # include <set> # include <string> using namespace STD; Map <string, int> color; int cube [5] [6]; int now [5] [6]; int N, ans; int P [7]; int CNT [25]; int dice24 [24] [6] =, 5, 2,, ,}; Int left [] = {,}; int up [] = }; // "Rotate" Void rot (int * t, int * P) {int Q [6]; memcpy (Q, P, sizeof (q )); for (INT I = 0; I <6; ++ I) P [I] = Q [T [I];} // generates void func () for the rotation sequence () {int P0 [6] = {0, 1, 2, 3, 4, 5}; printf ("int dice24 [24] [6] = {\ n"); For (INT I = 0; I <6; ++ I) {int P [6]; memcpy (p, P0, sizeof (P0); if (I = 0) Rot (up, P); if (I = 1) {rot (left, P); rot (up, P);} if (I = 3) {rot (up, P); rot (up, p) ;}if (I = 4) {rot (left, P); rot (left, P ); rot (up, P);} if (I = 5) {rot (left, P ); rot (up, p) ;}for (Int J = 0; j <4; ++ J) {printf ("% d, % d, % d, \ n ", P [0], p [1], p [2], p [3], p [4], P [5]); rot (left, p) ;}} printf ("};\ N") ;}// DFS brute force answer void DFS (INT cur) {If (cur> = N) {int counter = 0; For (INT I = 0; I <6; ++ I ){ Memset (CNT, 0, sizeof (CNT); int TMP = 0; For (Int J = 0; j <n; ++ J) {TMP = max (TMP, ++ CNT [now [J] [I]);} counter + = N-TMP;} ans = min (counter, ANS); return ;} for (INT I = 0; I <24; ++ I) {for (Int J = 0; j <6; ++ J) now [cur] [J] = cube [cur] [dice24 [I] [J]; DFS (cur + 1) ;}} int main () {char C [30]; int idx; while (~ Scanf ("% d", & N) {idx = 0; color. clear (); For (INT I = 0; I <n; ++ I) {for (Int J = 0; j <6; ++ J) {scanf ("% s", c); If (color. find (c) = color. end () {color [c] = ++ idx;} cube [I] [J] = color [c] ;}} ans = 10000000; for (INT I = 0; I <6; ++ I) now [0] [I] = cube [0] [I]; DFS (1 ); printf ("% d \ n", ANS);} return 0 ;}