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:
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.
- 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).
![](http://poj.org/images/1753_1.jpg)
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
bwwb bbwb bwwb bwww
Sample Output
4
题目大意:就是给你一个4*4的棋盘,通过翻转棋子来让整个棋盘变为全黑或全白,问几步可完成操作。
注意:翻转其某个棋子时,其上下左右都要做相应的翻转,即黑变白,白变黑。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 bool chess[6][6]= {{false}}; 5 int r[6]= {0,0,0,-1,1}; 6 int c[6]= {0,1,-1,0,0}; 7 int step; 8 bool flag; 9 bool reach_all() //判断是否为一种颜色 10 { 11 for(int i=1;i<5;i++) 12 { 13 for(int j=1;j<5;j++) 14 { 15 if(chess[i][j]!=chess[1][1]) 16 return false; 17 } 18 } 19 return true; 20 } 21 void turn(int row,int col) //翻棋 22 { 23 for(int i=0;i<5;i++) 24 chess[row+r[i]][col+c[i]]=!chess[row+r[i]][col+c[i]]; 25 return ; 26 } 27 void dfs(int row,int col,int deep) // 深搜迭代回溯很重要 28 { 29 if(deep==step) 30 { 31 flag=reach_all(); 32 return ; 33 } 34 if(flag||row==5) 35 return ; 36 turn(row,col); //翻棋 37 if(col<4) 38 dfs(row,col+1,deep+1); 39 else 40 dfs(row+1,1,deep+1); 41 turn(row,col); //不符合则翻回来 42 if(col<4) 43 dfs(row,col+1,deep); 44 else 45 dfs(row+1,1,deep); 46 return ; 47 } 48 int main() 49 { 50 char ch; 51 for(int i=1; i<5; i++) 52 { 53 for(int j=1; j<5; j++) 54 { 55 scanf("%c",&ch); 56 if(ch=='b') 57 chess[i][j]=true; 58 } 59 getchar(); //之前忘了写,就一直输出Impossible 60 } 61 for(step=0; step<=16; step++) //枚举每一种情况 62 { 63 dfs(1,1,0); 64 if(flag) 65 break; 66 } 67 if(flag) 68 printf("%d\n",step); 69 else 70 printf("Impossible\n"); 71 return 0; 72 }