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 ...
随机推荐
- mysql修改Truncated incorrect DOUBLE value:
UPDATE shop_category SET name = 'Secolul XVI - XVIII' AND name_eng = '16th to 18th centuries' WHERE ...
- strcpy、memcpy和memset的区别
strcpy 原型:extern char *strcpy(char *dest,char *src); 用法:#include <string.h> 功能:把src所指由NULL结束的字 ...
- Django框架介绍之cookie与session
cookie http请求时无状态的,一个客户端第一次,第二次,第n次访问同一个服务器都是一样的,服务器都会按照一个新的连接处理.但是,有时候客户端需要服务器记住客户端的登录状态,譬如离开一会,回来之 ...
- Cocos 开发笔记
经发现: cocos creator 提供的hello world 模版中.只有HelloWorkd.js中 properties 属性 text的值不是'hello world!' Label 组件 ...
- Ruby基础教程
一.Ruby基础知识 1.关于Ruby Ruby是脚本语言 Ruby是面向对象语言 Ruby是跨平台语言 Ruby是开放源码软件 2.Ruby入门书籍推荐 <Ruby.Programming向R ...
- Linux命令中:rsync和cp之间的区别
rsync:只拷贝那些更新的文件: cp -u:也可以实现类似效果: 两者都基本可以满足备份的需求: 只是一般情况下,用rsync做这类备份之类的事情,更多见: 在备份的操作中,拷贝,过期文件的删除是 ...
- ubuntu搭建discuz论坛
a.安装mysql database 1.安装mysql服务端 sudo apt-get install mysql-server (在此过程中要求为mysql的root用户设置一个密码) 2.安装 ...
- 【Hostname】Linux修改主机名称
环境:CentOS 6.5 方式一: 1.修改hosts文件 vi /etc/hosts 将本机ip地址后面的hostname改掉 2.修改network文件 vi /etc/sysconfig/ne ...
- UVa 10163 仓库守卫
https://vjudge.net/problem/UVA-10163 题意: 有n个仓库,m个管理员,每个管理员有一个能力值P(接下来的一行有m个数,表示每个管理员的能力值) 每个仓库只能由一个管 ...
- python 字符串转变量方法
1.response=eval('requests.'+func.lower())(destURL, headers=requestHeaders, data=postData, params=que ...