POJ 1753 Flip Game(bfs+位压缩运算)
http://poj.org/problem?id=1753
题意:一个4*4的棋盘,只有黑和白两种棋子,每次翻转一个棋子,并且其四周的棋子也跟着翻转,求棋盘全为黑或全为白时所需的最少翻转次数。
思路:暴力枚举。
一共16个棋子,所以可以用二进制来存储。后来看了一下别人的代码,发现居然可以用异或运算来计算翻转情况,方便了不少。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std; int t[] = { //对应翻转情况
, , , ,
, , , ,
, , , ,
, , , ,
}; int ans=;
bool vis[( << )]; struct node
{
int ans;
int step;
}; void bfs()
{
memset(vis, false, sizeof(vis));
queue<node>q;
node p;
p.ans = ans;
p.step = ;
q.push(p);
vis[ans] = true;
while (!q.empty())
{
p = q.front();
q.pop();
if (p.ans == || p.ans == )
{
cout << p.step << endl;
return;
}
for (int i = ; i<; i++)
{
node p1;
p1.step = p.step + ;
p1.ans = p.ans^t[i];
if (vis[p1.ans]) continue;
vis[p1.ans] = true;
q.push(p1);
}
}
cout << "Impossible" << endl;
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int cnt = ;
for (int i = ; i<; i++)
{
char ch[];
cin >> ch;
for (int j = ; j<; j++)
{
if (ch[j] == 'w')
{
ans += ( << cnt); //转化为十进制
}
cnt--;
}
}
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 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
- 枚举 POJ 1753 Flip Game
题目地址:http://poj.org/problem?id=1753 /* 这题几乎和POJ 2965一样,DFS函数都不用修改 只要修改一下change规则... 注意:是否初始已经ok了要先判断 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- 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(高斯消元+状压枚举)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45691 Accepted: 19590 Descr ...
- POJ 1753 Flip Game(状态压缩+BFS)
题目网址:http://poj.org/problem?id=1753 题目: Flip Game Description Flip game is played on a rectangular 4 ...
- poj 1753 Flip Game 枚举(bfs+状态压缩)
题目:http://poj.org/problem?id=1753 因为粗心错了好多次……,尤其是把1<<15当成了65535: 参考博客:http://www.cnblogs.com/k ...
- poj1753 Flip Game(BFS+位压缩)
题目链接 http://poj.org/problem?id=1753 题意 一个棋盘上有16个格子,按4×4排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...
随机推荐
- Jedis 对 Redis 的操作详解
1. JedisUtil2. 键操作3. 字符串操作4. 字节串4. 整数和浮点数5. 列表6. 集合(Set)7. 散列8. 排序sort 本篇主要阐述Jedis对redis的五大类型的操作:字符串 ...
- 005-四种常见的 POST 提交数据方式
1.http请求方法 HTTP Method RFC Request Has Body Response Has Body Safe Idempotent Cacheable GET RFC 7231 ...
- [py]python自省工具
参考 在日常生活中,自省(introspection)是一种自我检查行为.自省是指对某人自身思想.情绪.动机和行为的检查.伟大的哲学家苏格拉底将生命中的大部分时间用于自我检查,并鼓励他的雅典朋友们也这 ...
- MongoDB3.x中添加用户和权限控制
现在需要创建一个帐号,该账号需要有grant权限,即:账号管理的授权权限.注意一点,帐号是跟着库走的,所以在指定库里授权,必须也在指定库里验证(auth) ? 1 2 3 4 5 6 7 8 9 10 ...
- 1025 PAT Ranking[排序][一般]
1025 PAT Ranking (25)(25 分) Programming Ability Test (PAT) is organized by the College of Computer S ...
- ShuffleElements(随机打乱数组中的元素)
给定一个数组,随机打乱数组中的元素,题意很简单直接上代码: package Array; import java.util.Arrays; import java.util.Collections; ...
- 数据仓库基础(八)Informatica 小例子
本文转载自:http://www.cnblogs.com/evencao/p/3147843.html 之前看了一段数据库的基础,感觉自己对数据库的基础挺薄弱的.以后再学习其他东西的时候也需要经常能学 ...
- linux常用命令:service 命令
service命令用于对系统服务进行管理,比如启动(start).停止(stop).重启(restart).查看状态(status)等.相关的命令还包括chkconfig.ntsysv等,chkcon ...
- web前端----css属性
一.文本 1.文本颜色:color 颜色属性被用来设置文字的颜色. 颜色是通过CSS最经常的指定: 十六进制值 - 如: #FF0000 一个RGB值 - 如: RGB(255,0,0) 颜色的名称 ...
- 程序猿职场心理学,教你三进三出“斩”HR拿offer(跳槽必看)
摘要: 今天主要涉及到的是 HR 在面试时有哪些套路,这样可以见招拆招,斩获 offer! 今天主要涉及到的是 HR 在面试时有哪些套路,这样可以见招拆招,斩获 offer! 主要包括以下内容: 一. ...