【BFS|位运算+输出路径】POJ-2965 The Pilots Brothers' refrigerator
来源:互联网 发布:减速机选型软件 编辑:程序博客网 时间:2024/06/06 03:00
Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.
There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row iand all handles in column j.
The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.
Sample Input
-+-----------+--
Sample Output
61 11 31 44 14 34 4
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <climits>#include <iostream>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 1e5;struct Node {int m, step, dir;Node(){}Node(int _m, int _s, int _d):m(_m), step(_s), dir(_d){}};Node q[N];bool vis[0xFFFF+1];int flag, fa[N];Node pull(Node t, int i){Node u;int x = i >> 2, y = i & 3;//取出行和列//0123 4567 89AB CDEFx <<= 2;for(int k = 0; k < 4; k++) {t.m ^= 1 << (x+k);}for(int k = 0; k <= 12; k+=4) {t.m ^= 1 << (y+k);}t.m ^= 1 << i;u.m = t.m;u.dir = i;u.step = t.step + 1;return u;}int bfs(){int fron = 0, rear = 1; while(fron < rear) {Node t = q[fron];if(!t.m) {flag = fron;return t.step;}for(int i = t.dir + 1; i < 16; i++) {Node u = pull(t, i);if(!vis[u.m]) {vis[u.m] = 1;fa[rear] = fron;q[rear++] = u;}}fron++;}return -1;}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen(".out", "w", stdout);#endifchar s[4][10];int x = 0;for(int i = 0; i < 4; i++) {scanf("%s", s[i]);}for(int i = 3; i >= 0; i--) {for(int j = 3; j >= 0; j--) {if(s[i][j] == '+') {x = x << 1 | 1;}else x <<= 1;}}vis[x] = 1;q[0] = Node(x, 0, -1);fa[0] = -1;int ans = bfs();printf("%d\n", ans);while(~fa[flag]) {int tmp = q[flag].dir;printf("%d %d\n", (tmp>>2)+1, (tmp&3)+1);flag = fa[flag];}return 0;}
- 【BFS|位运算+输出路径】POJ-2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator BFS+位运算
- POJ-2965-The Pilots Brothers' refrigerator-bfs枚举-位运算
- POJ 2965-The Pilots Brothers' refrigerator(位运算+BFS+回溯路径)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+位运算)
- poj-2965-The Pilots Brothers' refrigerator(巧妙的位运算)
- poj_2965 The Pilots Brothers' refrigerator(bfs+位运算)
- poj2965 The Pilots Brothers' refrigerator(位运算)
- POJ 2965 The Pilots Brothers' refrigerator(BFS+二进制判重)
- BFS + 状态压缩 POJ 2965 The Pilots Brothers' refrigerator
- POJ 2965The Pilots Brothers' refrigerator (bfs)
- POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】
- poj 2965 The Pilots Brothers' refrigerator (状压+bfs)
- poj 2965 The Pilots Brothers' refrigerator bfs+状态压缩+路径回溯
- POJ 2965 The Pilots Brothers' refrigerator(dfs+记录路径)
- POJ 2965-The Pilots Brothers' refrigerator(枚举&&DFS&&输出过程)
- poj 2965 The Pilots Brothers' refrigerator
- POJ 2965 The Pilots Brothers' refrigerator
- SQL数据库索引
- 火狐浏览器小图标不显示
- 大数据将死
- Log4cplus入门
- Python 模拟简单的queue
- 【BFS|位运算+输出路径】POJ-2965 The Pilots Brothers' refrigerator
- raw_spin_lock与spin_lock
- static FFmpeg binaries for Mac OS X Intel 64bit
- 通过金矿模型介绍动态规划
- Xcode svn import项目 上传.a文件(静态库)
- 云计算和大数据应用技术免费讲座
- window2008下创建裸设备-为Oracle asm diskgroup准备
- 图像变换-使用Hough变换检测圆
- 查询未绑定变量的sql