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
题意:翻棋子,翻一个,相邻的也要翻(上下左右的),给出初始棋盘,问最少多少步能把棋盘变成全白OR全黑
搜索+状压:把每个棋子都翻一下,分别记录状态,再在这个基础上循环前面的步骤,
vis[i],把i变成二进制,则对应位上的数字代表棋盘对应位置上的棋子状态
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
typedef long long LL;
typedef long double LD;
using namespace std;
const int maxn=5;
char ma[maxn][maxn];
int vis[65536+10];
//int ff[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int f[5][2]={{-1,0},{1,0},{0,-1},{0,1},{0,0}};
int ff[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};
//int f[8][2]={{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{1,-2},{-1,2},{1,2}};
int N=4,M=4,K;
struct node
{
int status;
int step;
void setnode(int sta,int ss)
{
status=sta;
step=ss;
}
friend bool operator <(node a,node b)
{
return a.step>b.step;
}
};
priority_queue<node>q;
bool OK(int x,int y)//过界
{
if(x>=0&&y>=0&&x<N&&y<M)
return true;
return false;
}
int qf(int t,int x,int y)
{
int i=x*4+y;//要修改第i位
t^=ff[i];//对二进制第i位取反
return t;
}
void bfs(node st)
{
q.push(st);
while(!q.empty())
{
node t=q.top();
//printf("*****%d %d\n",t.status,t.step);
q.pop();
if(t.status==0||t.status==65535)
{
printf("%d\n",t.step);
return;
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)//翻(i,j)
{
int tem=t.status;
for(int k=0;k<5;k++)
{
int x=i+f[k][0];
int y=j+f[k][1];
if(!OK(x,y))continue;//越界
tem=qf(tem,x,y);//把(x,y)翻过来
}
// printf("%d %d\n",tem,t.step+1);
if(vis[tem]==0)
{
vis[tem]=1;
q.push((node){tem,t.step+1});
}
}
}
}
printf("Impossible\n");
}
int main()
{
node st,en;
while(~scanf("%s%s%s%s",ma[0],ma[1],ma[2],ma[3]))
{
//printf("%s%s%s%s\n",ma[0],ma[1],ma[2],ma[3]);
int s=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
s*=2;
if(ma[i][j]=='b')
s+=1;
}
}
//printf("%d\n",s);
node st;
st.setnode(s,0);
memset(vis,0,sizeof(vis));
while(!q.empty())q.pop();
vis[s]=1;
bfs(st);
}
return 0;
}