Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 34192 | Accepted: 14933 |
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).
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的棋盘,每次选一个位置,将该位置及其上下左右共5个位置的棋子翻转,问最少翻转多少次可以使16个棋子颜色一样。
由于棋盘已经固定为4*4的大小,所以可以用一个二进制数来表示当前整个棋盘的状态。
如:
bbww
bwbw
bbbb
wwww
可以用二进制数1100 1010 1111 0000表示,转换为十进制就是51952,则vis[51952]=1。然后bfs搜索最小步数即可。
//#pragma comment(linker,"/STACK: 1677721600")
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e5+50;
#define MAX_INT 0x3f3f3f
int vis[MAXN];
queue<int>q;
int change(int num,int n)
{
num^=1<<n;
if(n>=4)
num^=1<<(n-4);
if(n<=11)
num^=1<<(n+4);
if(n%4!=0)
num^=1<<(n-1);
if(n%4!=3)
num^=1<<(n+1);
return num;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
int s=0,ans=MAX_INT,cnt=0,scnt=0,last=1,now=0;
char t;
memset(vis,0,sizeof(vis));
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
scanf("%c",&t);
if(t=='b')
s=s*2+1;
else
s=s*2+0;
}
getchar();
}
vis[s]=1;
q.push(s);
while(!q.empty())
{
int num=q.front();
q.pop();
if(num==0||num==65535)
{
ans=scnt;
printf("%d\n",ans);
break;
}
for(int i=0;i<16;i++)
{
int after=change(num,i);
if(!vis[after])
{
vis[after]=1;
q.push(after);
now++;
}
}
cnt++;
if(cnt>=last)
{
last=now;
now=0;
cnt=0;
scnt++;
}
}
if(ans==MAX_INT)
{
printf("Impossible\n");
}
return 0;
}