POJ Flip Game(BFS + 位运算)
来源:互联网 发布:数据库培训哪家好 编辑:程序博客网 时间:2024/05/22 02:47
题目链接:http://poj.org/problem?id=1753
一个翻转会带动四周,求至少要几次才能全白或者全黑,不能输出Impossible。
总共也就1 << 16 种状态,用数的位来存储就可以了。然后bfs找最快的, x ^ (1 << j) 是将x的第j位取反,怎么取反,看看棋子的位置就可以了 。
#include<iostream>#include<cstring>#include<cmath>#include<cstdio>#include<algorithm>#include <queue>using namespace std;char mp[5][5];bool vis[1 << 17];int start = 0;struct node { int state, rds;};inline int solve(int x, int now) { now ^= 1 << x; if(x % 4 != 0) { now ^= 1 << (x-1); } if(x < 12) { now ^= 1 << (x+4); } if(x > 3) { now ^= 1 << (x-4); } if(x % 4 != 3) { now ^= 1 << (x+1); } if(!vis[now]) { vis[now] = 1; return now; } return -1;}void bfs() { queue<node> que; node cur; cur.state = start; cur.rds = 0; que.push(cur); vis[start] = 1; while (!que.empty()) { cur = que.front(); que.pop(); if(cur.state == 0 || cur.state == (1 << 16) - 1) { printf("%d\n", cur.rds); return ; } for (int i = 0; i < 16; i ++) { int tmp = solve(i, cur.state); if(tmp != -1) { node N; N.state = tmp; N.rds = cur.rds + 1; que.push(N); } } } printf("Impossible\n");}int main () { for (int i = 0; i < 4; i ++) { scanf("%s", mp[i]); for (int j = 0; j < 4; j ++) { start = (start << 1) + (mp[i][j] == 'b' ? 1 : 0); } } bfs(); return 0;}
0 0
- POJ Flip Game(BFS + 位运算)
- POJ 1753 Flip Game ------- BFS & 位运算
- 位运算-BFS-Flip Game-POJ 1753
- poj 1753 Flip Game(位运算+bfs)
- 【BFS|位运算】POJ-1753 Flip Game
- [ACM] POJ 1753 Flip Game (枚举,BFS,位运算)
- POj 1753--Flip Game(位运算+BFS)
- POJ 1753 - Flip Game (位运算 + BFS)
- [ACM] POJ 1753 Flip Game (枚举,BFS,位运算)
- POJ 1753 Flip Game(bfs+状态压缩+位运算)
- poj 1753 Flip Game (bfs+bit位运算 ---好题!)
- poj_1753 Flip Game(dfs+枚举)/(bfs+位运算)
- POJ1753:Flip Game(BFS、枚举、位运算)
- poj1753 Flip Game BFS+位运算
- #POJ1753#Flip Game(位运算+BFS)
- POJ 1753 Flip Game (枚举 + 位运算)
- POJ 1753 Flip Game(搜索+位运算)
- POJ 1753 Flip Game ------- DFS & 位运算 .
- 在线语音听写
- eclipse Kepler tomcat内存溢出解决方案
- 事务隔离级别
- leetcode: Add Digits
- POJ 3737 UmBasketella(三分模板)
- POJ Flip Game(BFS + 位运算)
- QT程序自适应屏幕大小
- DuiLib::CTreeViewUI 的基本用法
- bbs项目bug
- Android PopupWindow的简单使用
- hdoj 1028
- 二叉树差值最大(阿里2015)
- 自定义控件(支持模板)
- nucleo stmcubemx 按键中断测试