Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 52748 | Accepted: 22168 |
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
题目大意:给出一个4*4的棋盘,棋盘上有两种颜色的棋子,黑色和白色,现在有一种操作:当你选择一个棋子,那么他的上下左右的棋子就会改变他的颜色为其原本对立的颜色。
问经过最短几步后棋盘中的棋子能变为同一颜色。
思路:思路参考自https://blog.csdn.net/u013480600/article/details/25505697感谢大佬!!
因为棋盘4*4,所以我们直接把整个棋盘的16个棋子的状态作为程序中BFS的一个状态即可。我们把棋子从第一行到最后一行每个b看成一个二进制的1,所以最终总共有1<<16个不同的状态。
令vis[1<<16]来判断当前某个状态是否已经出现(其实vis数组可以不需要),令dist[1<<16]来记录从原始状态到当前某个状态的最小步数。
当要翻转(i,j)格子的时候,那么对应16位二进制数中的i*4+j位二进制位.(想想是不是).且(i,j)周围的4个对应格子也会相应的翻转,不过要注意(i,j)周围的4个格子不一定有位置.可能(i,j)是边界.
代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int vis[1<<16+1],dist[1<<16+1];
int dr[]={1,0,-1,0};
int dc[]={0,-1,0,1};
int BFS(int st)
{
if(st==0||st+1==(1<<16)) return 0;
queue<int> Q;
vis[st]=1;
dist[st]=0;
Q.push(st);
while(!Q.empty())
{
int s=Q.front();Q.pop();
for(int i=0;i<16;i++)
{
int ns=s;
int r=i/4,c=i%4;
ns^=1<<i;
for(int d=0;d<4;d++)
{
int nr=r+dr[d];
int nc=c+dc[d];
if(nr>=0&&nr<4&&nc>=0&&nc<4)
ns^=1<<(nr*4+nc);
}
if(ns==0 || ns+1==(1<<16)) return dist[s]+1;
if(vis[ns]==0)
{
Q.push(ns);
vis[ns]=1;
dist[ns]=dist[s]+1;
}
}
}
return -1;
}
int main()
{
char maze[5][5];
for(int i=0;i<4;i++)
scanf("%s",maze[i]);
int st=0;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(maze[i][j]=='b')
st=st|(1<<(i*4+j));
int ans=BFS(st);
if(ans==-1) printf("Impossible");
else printf("%d",ans);
return 0;
}