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排列,每个格子有两面,两面的颜色分别为黑色和白色,游戏的每一轮选择一个格子翻动,翻动该格子 ...
随机推荐
- 010-jdk1.8版本新特性二-Optional类,Stream流
1.5.Optional类 1.定义 Optional 类是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象. Optional 是个 ...
- RN例子,发送http请求,日期选择
发送http请求 let map = { method: 'post', headers: { token: '', 'Content-Type': 'application/json' }, bod ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- loadrunner怎么打印接口返回的参数
//首先使用web_reg_save_param方法保存服务器返回的参数,如下: web_reg_save_param ("S_respond","LB=",& ...
- Git 全局设置
Git 全局设置: git config --global user.name "ASxx" git config --global user.email "123456 ...
- Promise学习探究
学习熟知吧,原理还是继续吧 例子1: var isGeted; function getRet(){ return new Promise(function(resolve, reject) { // ...
- 80. Remove Duplicates from Sorted Array II(双指针)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- Linux服务器---ssh登录
Ssh登录 Ssh是建立在应用层和传输层的安全协议,专门为远程登录回话和其他网络服务提供安全性.利用ssh可以有效的防止远程管理中的信息泄露问题,同时ssh传输的数据是经过压缩的,可以加快传输 ...
- vue-cli使用
vue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目,GitHub地址是:https://github.com/vuejs/vue-cli 一.安 ...
- web前端----jQuery扩展(很重要!!)
1.jQuery扩展语法 把扩展的内容就可以写到xxxx.js文件了,在主文件中直接导入就行了. 用法1.$.xxx() $.extend({ "GDP": function () ...