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).
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
bwwb bbwb bwwb bwww
Sample Output
4
题意:
每次取一颗黑白棋子对其进行翻转,那么的它的上下左右四个方向的棋子也会跟着翻转。问至少需要操作多少次,可以使棋盘的棋子全为黑色或者白色。
题解:
首先可以得出一个结论,那就是操作过程中对每个棋子最多只翻转一次。那么4*4的棋盘最多只操作16次就能得到结果,棋盘的可能情况有2^16种。
我们可以DFS(递归)求解。
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; bool maze[5][5]; int st; int dx[5]={0,0,0,1,-1},dy[5]={0,1,-1,0,0}; bool flag; bool check()//判断棋盘是否全为同色 { for(int i=0;i<4;i++) for(int j=0;j<4;j++) { if(maze[i][j]!=maze[0][0]) return false; } return true; } void flip(int x,int y)//对棋子进行翻转 { for(int i=0;i<5;i++) { int nx=x+dx[i],ny=y+dy[i]; if(0<=nx&&nx<4&&0<=ny&&ny<4) maze[nx][ny]=!maze[nx][ny]; } } void dfs(int x,int y,int k) { if(k==st&&check())//k为翻转的次数 { flag=true; return ; } if(flag||x==4) return ; flip(x,y); if(y<3) dfs(x,y+1,k+1); else dfs(x+1,0,k+1); flip(x,y);//状态回溯 if(y<3) dfs(x,y+1,k); else dfs(x+1,0,k); } int main() { for(int i=0;i<4;i++) for(int j=0;j<4;j++) { char x; cin>>x; if(x=='b') maze[i][j]=true; else maze[i][j]=false; } flag=false; for(st=0;st<=16;st++)//枚举需要翻转的次数 { dfs(0,0,0); if(flag) break; } if(flag) cout<<st<<endl; else cout<<"Impossible"<<endl; return 0; }