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).
![](https://i-blog.csdnimg.cn/blog_migrate/14b4b3ec0b5261bea3a5ad9f1313252c.jpeg)
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
<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int vis[65540];
int f(int ss,int i) </span><span style="font-size:18px;">//用位运算来反转棋子</span><span style="font-size:18px;">
{
ss^=1<<(15-i); //反转棋子本身
if(i-4>=0) //反转棋子上边的那颗棋子
ss^=1<<(15-i+4);
if(i+4<=15) //反转棋子下边的那颗棋子
ss^=1<<(15-i-4);
if(i!=0&&i!=4&&i!=8&&i!=12) //反转棋子左边的那颗棋子
ss^=1<<(15-i+1);
if(i!=3&&i!=7&&i!=11&&i!=15) //反转棋子右边的那颗棋子
ss^=1<<(15-i-1);
return ss;
}
void bfs(int sum)
{
memset(vis,0,sizeof(vis)); //vis数组既用来计数也用来作为标记
queue<int>s;
s.push(sum);
while(!s.empty())
{
int x=s.front();
s.pop();
if(x==0||x==65535) //如果当前状态已全部变白或全部变黑则直接输出步数并返回
{
printf("%d\n",vis[x]);
return ;
}
for(int i=0; i<16; i++)
{
int p=f(x,i); //当前状态移动第i颗棋子得到的状态的二进制数
if(!vis[p]) //如果移动后的状态未被标记则标记此状态并将其压入队列
{
vis[p]=vis[x]+1;
s.push(p);
}
}
}
printf("Impossible\n"); //如果遍历完整个队列也没有符合要求的就输出</span><span style="font-size:18px;">Impossible</span>
}
<span style="font-size:18px;">int main()
{
char st[20];
int cnt=0;
for(int i=0; i<4; i++) //将整个棋盘的字符转换成二进制数字作为当前状态
{
scanf("%s",st);
for(int j=0; j<4; j++)
cnt=(cnt<<1)+(st[j]=='b'?1:0);
}
bfs(cnt); //搜索当前状态到想要的结果的所有状态并计数
return 0;
}</span>