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 ;
}

POJ-1753 Flip Game (BFS+状态压缩)的更多相关文章

  1. POJ 1753 Flip Game (状态压缩 bfs+位运算)

    Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...

  2. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  3. ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

    求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...

  4. 枚举 POJ 1753 Flip Game

    题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...

  5. HDU1429+bfs+状态压缩

    bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...

  6. BFS+状态压缩 hdu-1885-Key Task

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...

  7. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  8. hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

    Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Sub ...

  9. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  10. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

随机推荐

  1. Secure CRT 自动记录日志log配置

    SecureCRT8.0的下载地址下载地址: 链接:https://pan.baidu.com/s/1i5q09qH 密码:4pa2 配置自动log操作如下: 1.options ---> Se ...

  2. cisco路由器 三层交换机简单环境配置实例(图)

    出处:http://www.jb51.NET/softjc/56600.html cisco路由器&三层交换机简单环境配置实例 一.网络拓扑图: 二.配置命令: 1.路由器的配置: inter ...

  3. 01: RabbitMQ

    目录: 1.1 RabbitMq与Redis队列对比 1.2 在win7 64位机上安装RabbitMQ 1.3 RabbitMQ消息分发轮询 与 持久化 1.4 RabbitMQ 设定某个队列里最大 ...

  4. 01: tornado基础篇

    目录:Tornado其他篇 01: tornado基础篇 02: tornado进阶篇 03: 自定义异步非阻塞tornado框架 04: 打开tornado源码剖析处理过程 目录: 1.1 Torn ...

  5. 20145101《Java程序设计》第10周学习总结

    20145101<Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输. 计算机网络 路由器和交换机组成了核心的 ...

  6. 20145322何志威《网络对抗技术》Exp6 信息搜集技术

    20145322何志威<网络对抗技术>Exp6 信息搜集技术 实验内容 掌握信息搜集的最基础技能: (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发 ...

  7. Java序列化流-ObjectOutputStream、ObjectInputStream

    Java对象流的基本概念: 实例代码: 实体类User: import java.io.Serializable; /** * @author zsh * @company wlgzs * @crea ...

  8. 函数引用参数加const

    Fun(const Type& type); 在引用传递的时候,在函数内部改变参数,会改变参数实际值. 加上了const就不能被修改.

  9. 使用fragment添加底部导航栏

    切记:fragment一定要放在framlayout中,不然不会被替换完全(就是切换之后原来的fagment可能还会存在) main.xml <LinearLayout xmlns:androi ...

  10. Python3基础 函数 有参数有返回值 对传入的参数加1

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...