[POJ2965]The Pilots Brothers' refrigerator

[POJ2965]The Pilots Brothers' refrigerator

试题描述

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 i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

输入

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.

输出

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.

输入示例

-+--
----
----
-+--

输出示例

6
1 1
1 3
1 4
4 1
4 3
4 4

数据规模及约定

见“试题描述

题解

woc sb 测评机害得我这题都得卡常数。。。

暴搜。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <queue>
using namespace std;

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
	return x * f;
}

#define maxn 65546
#define oo 2147483647

int step[maxn], fa[maxn], cnt, Q[maxn], hd, tl;
struct Path {
	int x, y;
	Path() {}
	Path(int _, int __): x(_), y(__) {}
} ps[maxn], p2[maxn];
void BFS(int s) {
	memset(step, -1, sizeof(step));
	step[s] = 0;
	hd = tl = 0; Q[++tl] = s;
	while(hd < tl) {
		int u = Q[++hd];
		if(!u) break;
		for(int i = 0; i < 4; i++)
			for(int j = 0; j < 4; j++) {
				int v = u;
				v ^= (1 << i * 4 + j);
				for(int k = 0; k < 4; k++) v ^= (1 << i * 4 + k), v ^= (1 << k * 4 + j);
				if(step[v] < 0) fa[v] = u, ps[v] = Path(i, j), step[v] = step[u] + 1, Q[++tl] = v;
			}
	}
	return ;
}

int main() {
	int s = 0;
	char ts[10];
	for(int i = 0; i < 4; i++) {
		scanf("%s", ts);
		for(int j = 0; j < 4; j++)
			s |= ((ts[j] == '+') << i * 4 + j);
	}
	
	BFS(s);
	
	int u = 0; printf("%d\n", step[0]);
	cnt = 0;
	while(u != s) p2[++cnt] = ps[u], u = fa[u];
	for(int i = cnt; i; i--) printf("%d %d\n", p2[i].x + 1, p2[i].y + 1);
	
	return 0;
}

 

posted @ 2016-11-24 19:18  xjr01  阅读(199)  评论(0编辑  收藏  举报