Poj 1753/BFS

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.