POJ-1753 Flip Game (BFS+状态压缩)
Description
- 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
Output
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+状态压缩)的更多相关文章
- 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 ...
- 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 ...
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
- poj - 3254 - Corn Fields (状态压缩)
poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
随机推荐
- java多线程----线程池源码分析
http://www.cnblogs.com/skywang12345/p/3509954.html 线程池示例 在分析线程池之前,先看一个简单的线程池示例. 1 import java.util.c ...
- C# Http方式下载文件到本地类改进版
在上文基础上增加了远程文件是否存在和本地文件是否存在的判断. 类代码: using System; using System.Collections.Generic; using System.Lin ...
- 如何在Linux环境下通过uwgsi部署Python服务
部署python程序时常常会遇到同一台服务器上2.x和3.x共存的情况,不同应用需要使用不用的python版本,使用virtualenv创建虚拟环境能很好地解决这一问题. 首先,需要在服务器上安装vi ...
- Python入门之面向对象的多态
本章目录: 一.多态 二.多态性 三.鸭子类型 ============================== 一.多态 多态指的是一类事物有多种形态. 动物有多种形态:人,狗,猪. import ab ...
- 20145206邹京儒MSF基础应用
20145206邹京儒MSF基础应用 一.MS08_067漏洞渗透攻击实践 实验前准备 1.两台虚拟机,其中一台为kali,一台为windows xp sp3(英文版). 2.在VMware中设置两台 ...
- AP聚类算法
一.算法简介 Affinity Propagation聚类算法简称AP,是一个在07年发表在Science上的聚类算法.它实际属于message-passing algorithms的一种.算法的基本 ...
- vim常用的配置
.vimrc配置 syntax enable syntax on set relativenumber set autoindent set tabstop=4 set expandtab " ...
- 安卓开发 Activity入门
生命周期 Activity包含5种状态,涉及7种方法 1. 启动状态 2. 运行状态 *** 即使内存不足,Android先销毁栈底的Activity,来确保当前Activity正常运行 3. 暂停状 ...
- hdu 3415 Max Sum of Max-K-sub-sequence 单调队列优化DP
题目链接: https://www.cnblogs.com/Draymonder/p/9536681.html 同上一篇文章,只是 需要记录最大值的开始和结束的位置 #include <iost ...
- 实现分享功能插件2---jiathis分享插件应用
博主原创:未经博主允许,不得转载 在上一篇的博文中分享了如何用百度分享插件实现分享功能,现在展示用jiathis进行实现分享功能: 主要代码如下: <body> <div class ...