您的位置:首页 > 其它

POJ-1753:Flip Game

2017-08-30 16:12 435 查看
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


思路:bfs。因为只有16格,用1表示black,0表示white,可以用一个整数表示当前状态的地图,用map存下。

#include<queue>
#include<algorithm>
#include<map>
#include<cstdio>
#include<iostream>
using namespace std;
map<int,int>ma; //用map<状态,步数>ma来记录状态和步数
char s[6][6];
int a[6][6];
int d[4][2]={-1,0,0,-1,1,0,0,1};
int insert() //尝试插入当前状态的地图(判重)
{
int ans=0,k=1;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)ans+=k*a[i][j],k*=2; //转化为整数
}
if(ma[ans]==0)return ans; //插入成功,没有重复
return -1;
}
int bfs()
{
ma.clear();
queue<int>p;
int now=insert();
p.push(now);
ma[now]=1;
while(!p.empty())
{
int pre=p.front();p.pop();
if(pre==0||pre==((1<<16)-1))return ma[pre]-1;//如果全为0或全为1 就返回
for(int i=0,x=pre;i<4;i++) //把整数转化为01地图
{
for(int j=0;j<4;j++)a[i][j]=x%2,x/=2;
}
for(int i=0;i<4;i++) //对每个格子进行翻转
{
for(int j=0;j<4;j++)
{
for(int k=0;k<4;k++)
{
int nx=i+d[k][0];
int ny=j+d[k][1];
if(nx>=0&&nx<4&&ny>=0&&ny<4)a[nx][ny]^=1;
}
a[i][j]^=1;
int ans=insert();
if(ans!=-1)ma[ans]=ma[pre]+1,p.push(ans); //插入成功,放入队列
for(int k=0;k<4;k++)
{
int nx=i+d[k][0];
int ny=j+d[k][1];
if(nx>=0&&nx<4&&ny>=0&&ny<4)a[nx][ny]^=1;
}
a[i][j]^=1;
}
}
}
return -1;
}
int main()
{
while(scanf("%s",s[0])!=EOF)
{
scanf("%s%s%s",s[1],s[2],s[3]);
for(int i=0;i<4;i++) //把初始地图转化为01状态
{
for(int j=0;j<4;j++)a[i][j]=(s[i][j]=='b'?1:0);
}
int ans=bfs();
if(ans==-1)puts("Impossible");
else cout<<ans<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: