Flip Game
Time limit: 1000MS Memory Limit: 65536K
Description
Flip Game is played on a rectangular 4×4 field with two-sided pieces placed on each of its squares. One side of each piece are white and the other one are black and each piece is lying either it's black or white side up. Each round your 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 is chosen every round according to the following rules:
- Choose any one of the 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 is 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 was shown at the picture) and then the field would 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 is to write a program, that would 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" from each of the 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 and then write 0. If it's impossible to achieve the goal and then write the word "impossible" (without quotes).
Sample Input
BWWB
BBWB
BWWB
bwww
Sample Output
4
(1) The optimal solution relates only to the position of the flipped piece, regardless of the order of the reversal;
(2) for any piece, flip odd number of times equal to flip once, flip even several times equal and not flip;
(3) When a piece is determined, if there is a scheme, then the scheme is unique.
idea: The solution must be a certain pieces of a flip, through the deep search respectively enumeration of the number of flips 0, 1, 2 In the case of 16 o'clock, the total enumeration situation has 2^16=65536 species.
Accode:
#include <iostream>using namespacestd;Const intN =4;BOOLMap[n][n],flag;intStep;//Judging all white or All BlackBOOLjudge () {inti,j; for(i =0; i < N; i++ ) for(j =0; J < N; J + + ){ if(MAP[I][J]! = * *map)return false; } return true;} //Flip A pawnvoidFlipintIintj) {Map[i][j]= !Map[i][j]; if(I >0) Map[i-1][J] =!map[i-1][j]; if(I <3) Map[i+1][J] =!map[i+1][j]; if(J >0) Map[i][j-1] =!map[i][j-1]; if(J <3) Map[i][j+1] =!map[i][j+1]; } voidDfsintIintJintDP) { if(DP = =Step) {Flag=judge (); return; } if(Flag | | i = =N)return; Flip (i,j);//Flip if(J <3) DFS (i,j+1, dp+1); ElseDFS (i+1,0, dp+1); Flip (i,j);//Restore if(J <3) DFS (i,j+1, DP); ElseDFS (i+1,0, DP);} intMainvoid){ inti,j; CharC; for(i =0; i < N; i++ ) for(j =0; J < N; J + +) {cin>>C; if(c = ='b') Map[i][j]=1; } //enumeration Rollover Times for(step =0; Step <= -; step++) {DFS (0,0,0); if(flag) {cout<< Step <<Endl; return 0; }} cout<<"Impossible"<<Endl; return 0;}View Code
Test data:
Bwbw
Wwww
Bbwb
Bwwb
Impossible
Bwwb
Bbwb
Bwwb
bWWW
4
Wwww
Wwww
Wwww
Wwww
0
bbbb
bbbb
bbbb
bbbb
0
bbbb
Bwbb
bbbb
bbbb
Impossible
Bwbb
Bwbb
Bwbb
bbbb
Impossible
Bwbb
Wwwb
Bwbb
bbbb
1
Wwww
Wwwb
Wwbb
Wwwb
1
Wwww
Wwww
Wwwb
Wwbb
1
Wbwb
Bwbw
Wbwb
Bwbw
Impossible
bbbb
Bwwb
Bwwb
bbbb
4
Bwwb
Wbbw
Wbbw
Bwwb
4
Bbww
Bbww
Wwbb
Wwbb
Impossible
Bbwb
Bbbw
Wwbb
Wwwb
Impossible
Wwwb
Wwbw
Wbww
Wwbw
Impossible
bbbb
Wwww
Wwbb
Wbbb
Impossible
Bwwb
Wbwb
Wbbb
Wbbb
4
Bwbb
Bwbb
Bwbw
Bbbw
5
Wbwb
bbbb
Bbww
Wbbb
6
Bbwb
bbbb
Wbwb
bbbb
5
Poj_1753--flip Game (enum)