POJ 1753 Flip Game(状态压缩+BFS)
题目网址:http://poj.org/problem?id=1753
题目:
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
Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4 思路: 棋盘只有4*4即16格,我们把这16格的白棋黑棋状态分别用1,0表示,就可以用十进制的0——(2^16-1即65535)表示所有情况。利用0^1=1,1^1=0,0^0=0即无论0,1与0做^运算都等于其本身,无论0,1与1做^运算都等于另一个数的特性,我们将翻转棋子操作,变成将选定棋子和四周的棋子的状态^1,其余棋子状态^0。再用bfs进行搜索,用vis数组标记当前棋盘情况是否出现过,未出现则将当前情况入队,反之则不入。

代码:
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int vis[];
int mp[]={//预处理,事先算出翻转16个棋子 分别对应的"^"操作数
,,,,
,,,,
,,,,
,,,
};
char chess[][];
vector<int>v;
queue<int>q;
int change(){//将棋盘的初始状态压缩
int x=v[];
for (int i=; i<v.size(); i++) {
x=(x<<)+v[i];
}
return x;
}
int bfs(int v){
vis[v]=;
q.push(v);
while (!q.empty()) {
int x=q.front();q.pop();
if(x== || x== ) return vis[x]-;//x==0时,棋子全为黑面朝上,x==65535时,棋子全为白面朝上。vis数组保存的是当前步数+1
for (int i=; i<; i++) {//分别翻转16个棋子
int xt=x^mp[i];
if(vis[xt]) continue;
vis[xt]=vis[x]+;
q.push(xt);
}
}
return -;
}
int main(){
for (int i=; i<; i++) {
gets(chess[i]);
for (int j=; j<; j++) {
if(chess[i][j]=='b') v.push_back();
else v.push_back();
}
}
int x=bfs(change());
if(x!=-) printf("%d\n",x);
else printf("Impossible\n");
return ;
}
POJ 1753 Flip Game(状态压缩+BFS)的更多相关文章
- POJ 1753 Flip Game 状态压缩,暴力 难度:1
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4863 Accepted: 1983 Descript ...
- POJ - 1324 Holedox Moving (状态压缩+BFS/A*)
题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...
- POJ 3411 Paid Roads (状态压缩+BFS)
题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 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 ...
- 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 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
随机推荐
- MYSQL之查询篇
2. 数据库操作 数据库在创建以后最常见的操作便是查询 2.1 查询 为了便于学习和理解,我们预先准备了两个表分别是stduents表和classes表两个表的内容和结构如下所示 students表的 ...
- 微服务SpringCloud之注册中心Consul
Consul 介绍 Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul 的方案更“一站式”,内置了服务注册与发 ...
- VMbox 安装 LInux系统流程
STEP 1 文件--新建---(自定义高级)---(默认设置)---(稍后安装系统)---(Linux+选择版本)---(虚拟机名字+存放位置)---(处理器2+核数2)---(虚拟机内存)2G一般 ...
- impala对元数据的界面更新操作
执行 impala-shell 即能进入界面操作sql.如果在hive更新了数据之后,而在impala中却无法看到更新后的数据的话,意味着impala里元数据信息还没有刷新,此时在impala操作界面 ...
- Spring 梳理 -异常处理
Spring 提供了多种方式将异常转换为相应 Spring框架提供的通用异常,将异常转换为HTTP状态码 Spring默认会将自身抛出的异常自动映射到合适的状态码,如下是一些示例: 举个例子,当后端抛 ...
- Spring MVC-从零开始-未完待续
Spring MVC 之 json格式的输入和输出 Spring定时器简单使用 Spring mvc 拦截器的简单使用 Spring MVC文件上传
- 《你不知道的JavaScript》笔记(一)
用了一个星期把<你不知道的JavaScript>看完了,但是留下了很多疑惑,于是又带着这些疑惑回头看JavaScript的内容,略有所获. 第二遍阅读这本书,希望自己能够有更为深刻的理解. ...
- API离线查看工具【包括!!所用常用!!开发语言的API】
我相信对于每一个开发人员来说,都是不喜欢死记硬背API的,一些常用的方法或者接口我们是可以直接信手捏来的,或者说直接使用开发工具的自动代码提示,也能很快的找到自己想用的API,如果是自己从未使用过的, ...
- Vue2.0+ElementUI实现表格翻页的实例
参考地址: https://www.cnblogs.com/zhouyifeng/p/7706815.html
- Angular7 HttpClient处理多个请求
1. MergeMap - 串联请求 后一个请求需要前一个请求的返回结果时,需要使用串联请求. 可以使用MergeMap实现, 优势是减少嵌套,优化代码: 代码如下: import {HttpClie ...