您的位置:首页 > 其它

POJ 2965 The Pilots Brothers' refrigerator

2012-12-02 12:59 465 查看
The Pilots Brothers' refrigerator

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 13567Accepted: 5080Special 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

Source
Northeastern Europe 2004, Western Subregion
解题思路:和1753Flip Game基本一样,买一送一。1753的传送门点击打开链接
#include<iostream>
using namespace std;
char chess[6][6]={'*'};
bool Chess[6][6]={0};
int row[16]={0};
int col[16]={0};
bool flag;
void flip(int x,int y)
{
	int i,j;
	Chess[x][y]=!Chess[x][y];//先翻目标位置,这样接下来两次翻转让它总共翻3次,等价于翻一次
	for(i=1;i<=4;i++)
		Chess[i][y]=!Chess[i][y];//翻动目标所在列
	for(j=1;j<=4;j++)
		Chess[x][j]=!Chess[x][j];//翻动目标所在行
}
bool finish()
{
	int i,j;
	for(i=1;i<=4;i++)
		for(j=1;j<=4;j++)
			if(!Chess[i][j])
				return false;
	return true;
}
bool dfs(int x,int y,int now,int deep)
{
	if(now>deep)   return false;
	if(finish())   return true;
	for(int i=x;i<=4;i++)
		for(int j=y;j<=4;j++)
		{
			flip(i,j);
			row[now]=i;col[now]=j;
			if(j<4)
			{if(dfs(i,j+1,now+1,deep))  return true;}
			else
			{if(dfs(i+1,1,now+1,deep))  return true;}
			flip(i,j);
			if(j==4)
				y=1;
		}
	return false;
}
int main()
{
	int i,j,k,step;
	for(i=1;i<=4;i++)
		for(j=1;j<=4;j++)
		{
			cin>>chess[i][j];
			if(chess[i][j]=='-')
				Chess[i][j]=true;
		}
    for(step=0;step<=16;step++)//一共可翻动0,1,2....16个位置,共2^16种可能
		if(dfs(1,1,0,step))//起始目标位为(1,1),已翻动的位置数为0,可供翻动的位置数为step
			break;
	cout<<step<<endl;
	for(k=0;k<step;k++)
		cout<<row[k]<<" "<<col[k]<<endl;
		return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: