Flip game
| Time limit:1000 ms |
|
Memory limit:65536 K |
| Total submissions:31031 |
|
Accepted:13500 |
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:
- Choose any one of the 16 pieces.
- 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 Source Northeastern Europe 2000: There is a 4*4 square, with a piece of chess in each square. The chess piece is white and black on one side. The rule of the game is to select one of the 16 pieces each time, and turn the selected piece along with the pieces around it. When all the pieces are in the same color up, the game is complete. Given an initial state, you must output the minimum number of times the game is flipped to complete. If the initial state has reached the expected value, 0 is output. If the game cannot be completed, output impossible. Idea: Because the square is only 4*4, you can enumerate all States directly and implement it using the queue:# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <stack> # include <vector> # include <set> # include <queue> # pragma comment (linker, "/Stack: 102400000,102400000") # define maxn 1005 # define maxn 2005 # define mod 1000000009 # define INF 0x3f3f3f # define PI ACOs (-1.0) # define EPS 1e-6typedef long ll; using namespace STD; struct n Ode {int X; int step ;}; queue <node> q; int visit [70000]; // indicates whether int main () {int ID has occurred; char ch; id = 0; memset (visit, 0, sizeof (visit); For (INT I = 0; I <4; I ++) {for (Int J = 0; j <4; j ++) {scanf ("% C", & Ch); If (CH = 'B ') id = ID ^ (1 <(4 * I + J) ;}getchar () ;}if (ID = 0 | id = 65535) {printf ("0 \ n"); Return 0;} while (! Q. empty () Q. pop (); node St, now; ST. X = ID; visit [ID] = 1; ST. step = 0; q. push (ST); While (! Q. empty () {ST = Q. front (); q. pop (); If (St. X = 0 | St. X = 65535) {printf ("% d \ n", St. step); Return 0 ;}for (INT I = 0; I <4; I ++) for (Int J = 0; j <4; j ++) {now. X = ST. x ^ (1 <(4 * I + J); if (I> 0) now. X = now. x ^ (1 <(4 * (I-1) + J); // upper if (I <3) now. X = now. x ^ (1 <(4 * (I + 1) + J); // lower if (j> 0) now. X = now. x ^ (1 <(4 * I + J-1); // left if (j <3) now. X = now. x ^ (1 <(4 * I + J + 1); // right IF (! Visit [now. x]) {now. step = ST. step + 1; q. push (now); visit [now. x] = 1 ;}} printf ("impossible \ n"); Return 0 ;}/ * bwwbbbwbbwwbbwww */
|
Flip game (poj 1753) Enumeration + binary