Hdu_3278
Although there will be a lot of States if you consider three colors, if you decide which color we want to move to the middle, the remaining two colors can be regarded as one color, therefore, you can record the result from the BFS first, read a graph, and enumerate the three colors as the color to move to the middle, and then query the steps in O (1.
# Include <stdio. h> # Include < String . H> # Include <Algorithm> # Include <Queue> # Define Hash 1000007 # Define Maxd 1000010 # Define INF 0x3f3f3f Struct Point { Int St, D; point () {} Point ( Int _ St, Int _ D): ST (_ st), D (_ d ){}}; Struct Hashmap { Int Head [hash], size, next [maxd], St [maxd], d [maxd]; Void Init () {memset (Head, - 1 , Sizeof (Head), size =0 ;} Int Find ( Int State ){ Int I, H = State % Hash; For (I = head [H]; I! =- 1 ; I = Next [I]) If (St [I] = State) Return I; Return -1 ;} Void Push ( Int State, Int Dis ){ Int I, H = State % Hash; ST [size] = State, d [size] = DIS; next [size] = Head [H], head [H] = size ++ ;}} HM; Char B [ 5 ] [ 10 ]; Int ANS, code [ 5 ] [ 10 ]; Void Init (){ Int I, J; For (I = 0 ; I < 4 ; I ++) scanf ( " % S " , B [I]);} Int Encode (){ Int I, j, ANS = 0 ; For (I = 0 ; I < 4 ; I ++ ) For (J = 0 ; J < 6 ; J ++) ans = ans < 1 | Code [I] [J]; Return Ans ;} Void Decode ( Int St ){ Int I, J; For (I = 3 ; I> = 0 ; I -- ) For (J = 5 ; J> = 0 ; J --) Code [I] [J] = sT &1 , St> = 1 ;} Void SHR ( Int R, Int K ){ Int I; If (K = 0 ) For (I = 0 ; I < 5 ; I ++) STD: swap (code [r] [I], code [r] [I +1 ]); Else For (I = 5 ; I> = 1 ; I --) STD: swap (code [r] [I], code [r] [I- 1 ]);} Void SHC ( Int C, Int K ){ Int I; If (K =0 ) For (I = 0 ; I < 3 ; I ++) STD: swap (code [I] [c], code [I + 1 ] [C]); Else For (I = 3 ; I> = 1 ; I --) STD: swap (code [I] [c], code [I- 1 ] [C]);} Void Prepare (){ Int I, j, C; STD: queue <Point> Q; hm. INIT (); memset (code, 0 , Sizeof (CODE )); For (I = 1 ; I <= 2 ; I ++ ) For (J = 1 ; J <= 4 ; J ++) Code [I] [J] =1 ; C = Encode (); q. Push (point (c, 0 ), Hm. Push (C, 0 ); While (! Q. Empty () {point P = Q. Front (); q. Pop (); decode (P. St ); For (I = 0 ; I < 4 ; I ++ ) For (J = 0 ; J < 2 ; J ++ ) {SHR (I, j), c = Encode (); If (Hm. Find (c) =- 1 ) Q. Push (point (C, p. D + 1 ), Hm. Push (C, p. D + 1 ); SHR (I, j ^ 1 );} For (I = 0 ; I < 6 ; I ++ ) For (J = 0 ; J < 2 ; J ++ ) {SHC (I, j), c = Encode (); If (Hm. Find (c) =- 1 ) Q. Push (point (C, p. D + 1 ), Hm. Push (C, p. D + 1 ); SHC (I, j ^ 1 );}}} Void Deal ( Char Ch ){ Int I, j, C; For (I = 0 ; I < 4 ; I ++ ) For (J = 0 ; J <6 ; J ++) Code [I] [J] = B [I] [J] = Ch; c = Encode (); I = Hm. Find (c); ans = STD: min (ANS, hm. d [I]);} Void Solve (){ Int I, J; ans = INF; Deal ( ' B ' ), Deal ( ' W ' ), Deal ( ' G ' ); Printf ( " % D \ n " , ANS );} Int Main (){ Int T, TT; prepare (); scanf ( " % D " ,&T ); For (Tt = 1 ; TT <= T; TT ++ ) {Init (); printf ( " Case % d: " , TT); solve ();} Return 0 ;}