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

本文介绍了一款游戏中关于打开特殊冰箱门的谜题解决方法。该冰箱有16个开关,通过位压缩BFS算法找到最少操作次数及具体步骤来全部开启。文章详细解释了解题思路,并附上了实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值