首页 > 代码库 > POJ 1965 The Pilots Brothers' refrigerator 搜索

POJ 1965 The Pilots Brothers' refrigerator 搜索

The Pilots Brothers‘ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18222 Accepted: 6936 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 rowi 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


#include<iostream>
#include<cstring>
using namespace std;

int a[6][6];
int flag,ans;
int b[105][2];

int check()
{
	int i,j;
	for(i=1;i<=4;i++)
		for(j=1;j<=4;j++)
			if(a[i][j]!=1)
				return 0;
	return 1;
}

void change(int x,int y)
{
	int i;
	for(i=1;i<=4;i++)
	{
		a[i][y]=!a[i][y];
		a[x][i]=!a[x][i];
	}
	a[x][y]=!a[x][y];

	return ;
}

void dfs(int x,int y,int cur)
{
	if(cur==ans)
	{
		flag=check();
		return ;
	}
	if(x==5||flag)   return ;

	change(x,y);
	b[cur][0]=x;
	b[cur][1]=y;
	if(y<4)
		dfs(x,y+1,cur+1);
	else
		dfs(x+1,1,cur+1);

	change(x,y);
	if(y<4)
		dfs(x,y+1,cur);
	else
		dfs(x+1,1,cur);

	return ;
}


int main()
{
	char c;
	int i,j;
	flag=0;
	memset(a,0,sizeof(a));
	for(i=1;i<=4;i++)
	{
		for(j=1;j<=4;j++)
		{
			cin>>c;
			if(c=='-')
				a[i][j]=1;
		}
		getchar();
	}

	for(ans=0;ans<=16;ans++)
	{
		dfs(1,1,0);
		if(flag)
			break;
	}

	if(flag)
	{
		cout<<ans<<endl;
		for(i=0;i<ans;i++)
			cout<<b[i][0]<<" "<<b[i][1]<<endl;
	}

	return 0;
}