Bitwise computation-BFS-flip game-poj 1753

Source: Internet
Author: User

Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. one side of each piece is white and the other one is black and each piece is lying either it's black or white side up. each round you flip 3 to 5 pieces,
Thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
  1. Choose any one of the 16 pieces.
  2. Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any ).

Consider the following position as an example:

Bwbw
Wwww
Bbwb
Bwwb
Here "B" denotes pieces lying their black side up and "W" denotes pieces lying their white side up. if we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:

Bwbw
Bwww
Wwwb
Wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. you are to write a program that will search for the minimum number of rounds needed to achieve this goal.

Input

The input consists of 4 lines with 4 characters "W" or "B" each that denote game field position.

Output

Write to the output file a single integer number-the minimum number of rounds needed to achieve the goal of the game from the given position. if the goal is initially achieved, then write 0. if it's impossible to achieve the goal, then write the word "impossible"
(Without quotes ).

Sample Input

bwwbbbwbbwwbbwww

Sample output

4

Mark the number of sixteen Grids

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

You can use binary to record different states.

0 1 0 0 0 1 1 0 0 1 1 0 1
1 1

15 14 13 12 11 10 9 8 7 6 5 4 3 2
1 0

Now we have recorded

14 10 9 5 4 2 1 0

Finally, save the status in decimal format. 1 indicates 'B', 0 indicates 'w', and then flip it accordingly. The values are 0-> 1, 1-> 0.

# Include <iostream> # include <cstring> using namespace STD; char ma [5] [5]; // used to input int States [65535] = {0 }; // (core part) the record status is 65535int step [65535] = {0} because the maximum value is 2 ^ 16-1 = 65535 }; // number of record steps int rear = 0, front = 0; // record header and tail bool vis [65535]; // used to record whether search has been performed // input, and record the initial status, place void shuru () {int I, state = 0; for (I = 0; I <4; I ++) {CIN> ma [I]; int J; For (j = 0; j <4; j ++) if (MA [I] [J] = 'B ') state | = 1 <(I * 4 + J);} States [rear ++] = State; // records the initial state into the first vis [stat E] = true; // The record has passed} // reverse one and the int fanzhuan (INT stat, int I) {int state = 0; state | = 1 <I; if (I % 4! = 0) state | = 1 <(I-1); If (I + 1) % 4! = 0) state | = 1 <(I + 1); If (I-4> = 0) state | = 1 <(I-4); if (I + 4 <16) state | = 1 <(I + 4); Return (State ^ Stat);} // traverses the BFS width, searches for each State, and keeps putting it in the queue, returns true (true) bool BFS () {While (front <rear) {int state = States [Front ++]; // retrieves the queue header, record the status at this time if (0 = state | 65535 = State) // once found, return {cout <step [State] <"\ n "; return true;} int I; for (I = 0; I <16; I ++) // traverses each State {int st; ST = fanzhuan (state, I ); if (! Vis [st]) // prevents repeated traversal {States [rear ++] = sT; // It cannot be added to the end of the queue. vis [st] = true; step [st] = step [States [Front-1] + 1; // step count plus 1 }}return false;} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt", "W", stdout); shuru (); If (! BFS () cout <"impossible \ n"; 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.