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:
  1. Choose any one of the 16 pieces.
  2. 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

题目大意:在一个4x4的棋盘上,摆满了黑白两种颜色的棋子。当翻转一颗棋子的时候,与它相邻的上下左右四个棋子也随之翻转。问将整个棋盘置为同一种颜色最少需要翻转几次。
题目分析:这道题的数据量不大且固定不变,2^16个状态,又因为让找最小步骤数,所以就敲定用BFS。接下来就是状态如何表示了,很显然了,4行用四个不超过16的二进制数表示即可。至于状态转移,用位运算异或就能轻松完成。 代码如下:
 # include<iostream>
# include<cstdio>
# include<queue>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int a,b,c,d,t;
node(){}
node(int a1,int b1,int c1,int d1,int t1):a(a1),b(b1),c(c1),d(d1),t(t1){}
bool operator < (const node &a) const {
return t>a.t;
}
};
char p[][];
int vis [][][][];
void bfs()
{
int temp[],a,b,c,d;
for(int i=;i<;++i){
temp[i]=;
for(int j=;j<;++j){
if(p[i][j]=='b')
temp[i]=temp[i]*+;
else
temp[i]=temp[i]*+;
}
}
priority_queue<node>q;
memset(vis,,sizeof(vis));
vis[temp[]][temp[]][temp[]][temp[]]=;
q.push(node(temp[],temp[],temp[],temp[],));
while(!q.empty()){
node u=q.top();
q.pop();
if(u.a==&&u.b==&&u.c==&&u.d==){
printf("%d\n",u.t);
return ;
}
if(!u.a&&!u.b&&!u.c&&!u.d){
printf("%d\n",u.t);
return ;
}
for(int i=;i<;++i){
a=u.a,b=u.b,c=u.c,d=u.d;
a^=(<<i);
b^=(<<i);
if(i>)
a^=(<<(i-));
if(i<)
a^=(<<(i+));
if(!vis[a][b][c][d]){
vis[a][b][c][d]=;
q.push(node(a,b,c,d,u.t+));
}
}
for(int i=;i<;++i){
a=u.a,b=u.b,c=u.c,d=u.d;
b^=(<<i);
a^=(<<i);
c^=(<<i);
if(i>)
b^=(<<(i-));
if(i<)
b^=(<<(i+));
if(!vis[a][b][c][d]){
vis[a][b][c][d]=;
q.push(node(a,b,c,d,u.t+));
}
}
for(int i=;i<;++i){
a=u.a,b=u.b,c=u.c,d=u.d;
c^=(<<i);
b^=(<<i);
d^=(<<i);
if(i>)
c^=(<<(i-));
if(i<)
c^=(<<(i+));
if(!vis[a][b][c][d]){
vis[a][b][c][d]=;
q.push(node(a,b,c,d,u.t+));
}
}
for(int i=;i<;++i){
a=u.a,b=u.b,c=u.c,d=u.d;
d^=(<<i);
c^=(<<i);
if(i>)
d^=(<<(i-));
if(i<)
d^=(<<(i+));
if(!vis[a][b][c][d]){
vis[a][b][c][d]=;
q.push(node(a,b,c,d,u.t+));
}
}
}
printf("Impossible\n");
}
int main()
{
for(int i=;i<;++i)
scanf("%s",p[i]);
bfs();
return ;
}

最新文章

  1. Smart_Script
  2. iOS 单元测试之XCTest详解(一)
  3. 利用GCC编译器生成动态链接库和静态链接库
  4. INFORMATICA 的部署实施之 BACKUP&amp;RESTORE
  5. WordPress Tweet Blender插件跨站脚本漏洞
  6. 用java代码发送http请求
  7. CSDN博客投票活动开始了
  8. P2598 [ZJOI2009]狼和羊的故事(网络流)
  9. 大面积project.pbxproj冲突问题解决
  10. JavaSE| 集合
  11. java接口入参模板化,适用于企业化服务远程调度模板化的场景,接口入参实现高度可配置化
  12. paramiko 实现ssh登录和sftp登录
  13. TCP/IP 协议图--TCP/IP 基础
  14. tomcat启动 报org.apache.catalina.LifecycleException异常
  15. AngularJS 和 Electron 构建桌面应用
  16. windows配置redis(转)
  17. Tensorflow样例代码分析cifar10
  18. unity状态机实现
  19. 【week2】 构建之法 读后感及问题
  20. stegsolve的功能

热门文章

  1. 虚拟机中安装mac系统
  2. Linux中Postfix虚拟用户及虚拟域(六)
  3. python的time时间模块
  4. 20145310 《网络对抗》 MSF基础应用
  5. 20145314郑凯杰《网络对抗技术》恶意DLL注入进程(进程捆绑)的实现
  6. Python3基础 os mkdirs 创建多层文件夹
  7. Android 开发环境配置图文教程(jdk+eclipse+android sdk)
  8. 卸载vs2017
  9. 【Android实验】线程的使用-计时器
  10. js分号的重要性