Poj 1753 flip game (BFS + bit operations-Good question !)

Source: Internet
Author: User
Tags rounds
Flip game
Time limit:1000 ms   Memory limit:65536 K
Total submissions:25323   Accepted:10927

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
Goal, then write the word "impossible" (without quotes ).

Sample Input

bwwbbbwbbwwbbwww

Sample output

4

Source

Northeastern Europe 2000.

 

Question: Turn the pawns. There are two kinds of pawns: Black and White. For an initial state, ask if you can make all the pawns on the board black or white by turning over the pawns. If yes, output the number of steps to achieve the goal. Otherwise, output "impossible ".

The rules for turning a piece: each time you flip a piece and its upper and lower right pieces, it means "white turns black, black turns white ".

Analysis: search to simulate the status of each piece. Then compare it with all black and white.

The bitwise operation is because the chess piece has only two colors, which are represented by 0 and 1, and can be simulated by bitwise operations. A 4*4 lattice can be regarded as a hexadecimal number. However, to determine the status of all 0 and all 1

It is convenient to use the last 16 digits of the decimal int to simulate the current node status.

Thoughts: 1. I figured out the differences between hexadecimal notation and hexadecimal notation. 0 is the same representation.

2. Consolidate the application of bitwise operations: use X | 1 for position 1, and use x ^ 1 for a specific operation.

3. more skillful in simulating Queues with arrays.

4. There is also the transformation of rows and columns and coordinates, and the use of Division and Division is also very profound.

Code:

# Include <cstdio> # include <iostream> # include <cstring> using namespace STD; struct node {int X; // The Last 16 digits of the decimal integer represent the status int steps of the node; // record the number of steps int I that have reached the node; // indicates which piece has reached the current status, avoid returning previous statuses}; bool vis [100000] = {false}; char STR [10]; node queue [100000]; void flip (int I, node, node & B) // I = ID of the chess piece to be turned (0-15). A is the form parameter of queue [p, B is a reference of queue [Q] {int r = I/4, C = I % 4; // R and C are the row and column coordinate B of the I-th piece to be turned. X =. x; // in order not to change the status of the previous record, copy it to B first. Turn over the pawns and record the new status. B. X = B. x ^ (0x1 <I); // use ^ 0x1 to return the inverse (0 x1 represents 1 in hexadecimal notation) if (r> 0) B. X = B. x ^ (0x1 <(I-4); // flip I corresponds to the position above the pawn if (r <3) B. X = B. x ^ (0x1 <(I + 4); // If (C> 0) B. X = B. x ^ (0x1 <(I-1); // flip I corresponds to the position of the Left piece if (C <3) B. X = B. x ^ (0x1 <(I + 1); // flip the right part B of the position corresponding to I. I = I; B. steps =. steps + 1;} void Init () {queue [0]. X = 0; // initialize to a hexadecimal 0 (the same as the hexadecimal 0) queue [0]. steps = 0; queue [0]. I =-1; for (INT I = 0; I <4; I ++) {scanf ("% s", STR); For (Int J = 0; j <4; j ++) {If (STR [J] = 'B') // set the location of the black game to 1 (with | 0x1 set 1) queue [0]. X = (queue [0]. X | (0x1 <I * 4 + J) ;}} void BFS () {int p = 0, q = 0; while (! (Queue [Q]. X = 0 | queue [Q]. X = 0 xFFFF) // if the status of all 0 or all 1 is not met {for (INT I = 0; I <16; I ++) {If (queue [p]. I = I) continue; q ++; // The tail pointer is moved back to open up a new record space for the new State flip (I, queue [p], queue [Q]); if (vis [queue [Q]. x]) Q --; vis [queue [Q]. x] = true; If (queue [Q]. X = 0 | queue [Q]. X = 0 xFFFF) break;} If (P = q) {printf ("impossible \ n"); break;} p ++; // The header pointer moves one bit behind and uses the current state as the initial state of the search} If (queue [Q]. X = 0 | queue [Q]. X = 0 xFFFF) printf ("% d", queue [Q]. steps) ;}int main () {Init (); BFS (); Return 0 ;}

 

 

11914680

Fukan

1753

Accepted

268 K

32 Ms

C ++

1490b

2013-08-03 21:55:14

You can also use iterations to deepen DFS ....!!!

 

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.