Flip Game
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 24198 |
|
Accepted: 10409 |
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:
- 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
Source
Northeastern Europe 2000
BFS + 位运算
用16位2进制数来表示当前状态,共65535种状态
暴搜可得
0 ^ 1 = 1;
1 ^ 1 = 0;
位运算实现翻棋子。
根据输入要求,b代表黑棋(black),w代表白棋(white)。因为总共才16个位置,且只有黑白两种表示,此时,可对每一次状态进行二进制压缩(其中b代表1,w代表0),例如:
bwwb
bbwb
bwwb
bwww
即可表示为1001 1101 1001 1000,其十进制值为40344。同时,计算可知根据黑白棋的摆放情况,总共有2^16种不同的状态。每一次经过有效的操作后,状态都会发生改变,此时,可借助二进制位运运算实现状态的改变,即对原有状态(相应的十进制表示)进行异或操作,以此来改变其对应二进制数的相关位置的值(1变0,0变1)。
例如:
先假设前一个状态为:
wwww
wwww
wwww
wwww
即二进制表示为0000 0000 0000 0000,十进制对应为0。若此时选定左上角第一个棋子进行操作,根据规则,它右边和下边的也要同时进行变换(因为其左边和上边为空,不做考虑),之后,相应的状态用二进制表示,应变为:1100 1000 0000 0000,十进制值为51200。这个过程相当于对十进制数51200进行对十进制数0的异或操作,即next=0^(51200),而51200这个数则可以根据对十进制数1进行相应的左移操作得到。同时,我们知道,棋牌总共有16个位置,也就是说相应的不同的操作也有16种,即有16个不同的数经过异或操作用来改变前一个状态的值。那么,就先将这16个数枚出来吧
#include <iostream>
#include <cstdio>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char a[5][5];
bool vis[65535];
int change[16] = {51200,58368,29184,12544,35968,20032,10016,4880,2248,1252,626,305,140,78,39,19};
struct Node
{
int state;
int step;
};
int bfs(int state)
{
memset(vis , false , sizeof(vis));
queue<Node>q;
Node cur , next;
cur.state = state;
cur.step = 0;
q.push(cur);
vis[state] = true;
while(!q.empty())
{
cur = q.front();
q.pop();
if(cur.state == 0||cur.state == 65535)
{
return cur.step;
}
for(int i = 0 ; i < 16 ; i++)
{
next.state = cur.state ^ change[i];
next.step = cur.step + 1;
if(vis[next.state])continue;
if(next.state == 0||next.state == 65535)return next.step;
vis[next.state] = true;
q.push(next);
}
}
return -1;
}
int main()
{
int state , ans;
while(scanf("%s",&a[0])!=EOF)
{
for(int i = 1 ; i < 4 ; i++)
{
scanf("%s",a[i]);
}
state = 0;
for(int i = 0 ; i < 4 ; i++)
{
for(int j = 0 ; j < 4 ; j++)
{
state <<= 1;
if(a[i][j] == 'b')
{
state++;
}
}
}
ans = bfs(state);
if(ans == -1)printf("Impossible\n");
else printf("%d\n",ans);
}
return 0;
}