【BFS|位运算+输出路径】POJ-2965 The Pilots Brothers' refrigerator

来源:互联网 发布:减速机选型软件 编辑:程序博客网 时间:2024/06/06 03:00
The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536K    Special Judge

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
————————————————————悲しみの分割線————————————————————
前言:又被学弟抢先A了,血虐。。。悲哀。。。我天天在干嘛?!??!??!一个星期才做了几题??!??!?!?打脸!啪!。。。
思路:只是一道位压缩的BFS而已,做了这么久。路径输出本来就不难,我竟然SB地记录枚举的路径!那不肯定是0~15吗?!???!把队列转移路径记录下来!
代码如下:
/*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;}


0 0
原创粉丝点击