Method: 1. enumeration (used here)
Ii. Use binary to record subscript (not implemented yet)
Iii. analogy to playing cube games (the idea is Huang Chao, which has not yet been implemented)
Original question link: http://poj.org/problem? Id = 1753
Flip game
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:21024 |
|
Accepted:9108 |
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 question: Give You A 4*4 Board with only white (W) and Black (B) colors on it. If the front is white, the opposite is black, and vice versa. Now let you flip the board and ask the minimum number of times to make the last Board color uniform (that is, all is white or all is black ). If the flip fails, impossible is output; otherwise, the minimum number of successful flip attempts is output. Note: If you flip a checker's grid, the upper, lower, and lower sides of the grid are all flipped. Algorithm: enumeration + DFS + backtracking
# Include <stdio. h ># include <iostream> using namespace STD; int chess [4] [4]; int c = 33; void build () // mark the color of the Board {char C; int I, j; for (I = 0; I <4; I ++) for (j = 0; j <4; j ++) {CIN> C; If (C = 'W') chess [I] [J] = 0; else chess [I] [J] = 1 ;}} void turn (INT X, int y) // flip {If (x> = 0 & x <= 3 & Y> = 0 & Y <= 3) chess [x] [Y] =! Chess [x] [Y];} void flip (INT s) // a piece of chess piece changes, and the four around it changes {int I = s/4; // row Int J = S % 4; // column turn (I, j); turn (I + 1, J); turn (I, j + 1 ); turn (I-1, J); turn (I, J-1);} int complete () // judge whether the Board turns into the same color {int I, j, S1 = 0; for (I = 0; I <4; I ++) for (j = 0; j <4; j ++) S1 + = chess [I] [J]; if (S1% 16) return 0; else return 1;} void DFS (int s, int B) // perform a deep search. s represents the current square, and B represents the number of squares to flip {If (complete () // if it is the same color, find the final state {If (C> B) C = B; return;} If (S> = 16) // if return is traversed, DFS (S + 1, B); flip (s); DFS (S + 1, B + 1); flip (s);} int main () {build (); // mark the color of the board to DFS (); If (C = 33) // The maximum number of flipped requests is 4*4*2 = 32 printf ("impossible \ n"); else printf ("% d \ n", C ); return 0 ;}