Description
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 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 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 lines with 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 . If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
Source
第一种方法是bfs+状态压缩,将整张图压缩为01状态,算出二进制状态的十进制的值。然后跑bfs,bfs里面是枚举16个位置的变换,( 这里预处理出变换的值,然后进行异或计算),再加个标记vis不就行了。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000000
#define inf 1e12 int vis[(<<)];
struct Node{
int val;
int t; }st;
int mp[]={
,,,,,,,,,,,,,,,
};
void bfs(){
queue<Node>q;
q.push(st);
Node t1,t2;
vis[st.val]=;
while(!q.empty()){
t1=q.front();
q.pop();
if(t1.val== || t1.val==((<<)-)){
printf("%d\n",t1.t);
return ;
}
for(int i=;i<;i++){
t2=t1;
t2.val^=mp[i];
if(vis[t2.val]) continue;
vis[t2.val]=;
t2.t++;
q.push(t2);
}
}
printf("Impossible\n");
}
int main()
{
char s[];
int ans=;
int num=;
for(int i=;i<;i++){
scanf("%s",s);
for(int j=;j<;j++){
if(s[j]=='b'){
ans+=(<<num);
}
num--;
}
}
st.val=ans;
st.t=;
memset(vis,,sizeof(vis));
bfs(); return ;
}
第二种方法是dfs枚举。
思路:仔细考虑,可以发现,一个位置的棋子,要么保持原状不变,要么改变一次。因为改变偶数次和不改变的效果是一样的,改变奇数次和改变1次的效果是一样的。也就是说,达到最后颜色都一样的状态,某个位置的棋子,要么改变一次,要么一次也不改变。所以求最少经过多少步的改变,可以转化为最少需要改变多少个棋子。因为数据范围小,总共16个棋子,所以我们可以枚举。
我们可以判断改变0个,改变1个,改变2个,,,改变16个。改变x个,就是从16个元素中选x个的过程,可以用dfs实现。如果一直到最后改变16个都不行,则输出“Impossible”即可。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
using namespace std;
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 26
#define inf 1e12
int mp[][];
int newMp[][];
int newRecord[N];
int dirx[]={,,-,};
int diry[]={-,,,};
int flag;
int vis[N];
void judge(int cnt){
for(int i=;i<;i++){
for(int j=;j<;j++){
newMp[i][j]=mp[i][j];
}
}
for(int i=;i<cnt;i++){
int x=newRecord[i]/;
int y=newRecord[i]-x*;
newMp[x][y]=!newMp[x][y];
for(int j=;j<;j++){
int newX=x+dirx[j];
int newY=y+diry[j];
if(newX< || newX>= || newY< || newY>=) continue;
newMp[newX][newY]=!newMp[newX][newY];
}
}
int ans=;
for(int i=;i<;i++){
for(int j=;j<;j++){
if(newMp[i][j]){
ans++;
}
}
}
if(ans== || ans==){
flag=;
}
}
void dfs(int st,int num,int cnt){
if(num==cnt){
judge(cnt);
return;
} for(int i=st;i<;i++){
if(!vis[i]){
vis[i]=;
newRecord[num]=i;
dfs(i,num+,cnt);
if(flag)
return;
vis[i]=;
}
} }
int main()
{
char s[];
for(int i=;i<;i++){
scanf("%s",s);
for(int j=;j<;j++){
if(s[j]=='b'){
mp[i][j]=;
}
else{
mp[i][j]=;
}
}
}
flag=;
for(int i=;i<=;i++){//改变多少个可以达到目的
memset(vis,,sizeof(vis)); dfs(,,i);
if(flag){
printf("%d\n",i);
break;
}
}
if(flag==){
printf("Impossible\n");
} return ;
}