Black and white game flip
The 4x4 Board has 2 ^ 16 States. If the status value is 0 or 65535AlgorithmEnd
Level 1: initial status
Layer 2: initial status turning 0, initial status turning 1, initial status turning ...... Initial status increased by 15
Layer 3 :...............................
If the status value has not been accessed
# Include <iostream> # define max_state 65535 # define all_black 65535 # define all_white 0 # define width 4 # define heigth 4 # define size_of_board width * heigth # include <queue> Using Namespace STD; Int Flip ( Int Current, Int Pos) {current ^ = 1 <Pos; If (Pos> = width) Current ^ = 1 <(POS-width );If (Pos + width <size_of_board) Current ^ = 1 <(Pos + width ); If (Pos % width! = 0) Current ^ = 1 <(pos-1 ); If (Pos % width! = Width-1) Current ^ = 1 <(Pos + 1 ); Return Current ;} Int Main ( Int Argc, Char * Argv []) { Int Step [max_state]; Int Current_state = 0, next_state; // For (INT I = 0; I //{ // For (Int J = 0; j <width; j ++) // Current_state + = (getchar () = 'B') <(I * width + J ); // Getchar (); // //} // Debug ("% d", 8 ^ 2 ); Char C; For ( Int I = 0; I <size_of_board; I ++) {CIN> C; current_state + = (C = 'B') <(I ); // Debug ("% d", C ); } If (Current_state = all_white | all_black = current_state) {printf (" 0 \ n ");Return 0;} memset (step,-1, max_state); queue < Int > Q; q. Push (current_state); Step [current_state] = 0; // Debug ("% d", current_state ); While (! Q. Empty () {current_state = Q. Front (); q. Pop (); // Debug ("% d", current_state ); For ( Int I = 0; I <size_of_board; I ++) {next_state = flip (current_state, I ); If (Next_state = all_white | next_state = all_black) {printf (" % D \ n ", Step [current_state] + 1 ); Return 0 ;}If (-1 = step [next_state]) {step [next_state] = step [current_state] + 1; q. Push (next_state) ;}} printf (" Impossible \ n "); // Debug ("% d", current_state ); Return 0 ;}