POJ 2965 The Pilots Brothers' refrigerator

The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30872   Accepted: 11961   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 i and 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

【题意】

有4×4的棋盘,上面的棋子一面是黑的,一面是白的。规定翻转一个棋子的同时也要翻转它的同行同列的棋子,问给定一个棋盘的棋子状态,至少需要翻转多少个棋子,能使得所有棋子朝上都是白的。

输出最少数,然后依次输出要翻转的棋子的位置(任意一种可行方案皆可)

 

【分析】

除了压缩的状态不同,其余做法与POJ 1753 Flip Game(点击查看思路)完全相同。

 

【代码】

#include<cstdio>
#include<iostream>
#define debug(x) cerr<<#x<<" "<<x<<'
';
using namespace std;
const int N=105;
char s[N];int now,state[]={63624,62532,61986,61713,36744,20292,12066,7953,35064,17652,8946,4593,34959,17487,8751,4383};
int top,st[N];
inline void Init(){
	for(int i=0;i<4;i++){
		scanf("%s",s);
		for(int j=0;j<4;j++){
			now<<=1;
			now|=s[j]=='+';
		}
	}
}
inline bool check(){
	return !now;
}
inline void flip(int t){
	now^=state[t];
}
bool find(int n,int i){
	if(!n) return check();
	for(;i<16;i++){
		if(16-i<=n-1) break;
		flip(i);st[++top]=i;
		if(find(n-1,i+1)) return 1;
		flip(i);top--;
	}
	return 0;
}
inline void Solve(){
	for(int i=0;i<=16;i++){
		top=0;
		if(find(i,0)){
			printf("%d
",i);
			for(int j=1,x,y;j<=top;j++){
				x=st[j]/4+1,y=st[j]%4+1;
				printf("%d %d
",x,y);
			}
			return ;
		}
	}
	puts("Impossible");
}
int main(){
	Init();
	Solve();
	return 0;
}
 


 

 

原文地址:https://www.cnblogs.com/shenben/p/10367214.html