当前位置: 代码迷 >> 综合 >> The Pilots Brothers' refrigerator POJ - 2965 解题报告
  详细解决方案

The Pilots Brothers' refrigerator POJ - 2965 解题报告

热度:42   发布时间:2024-01-13 03:57:56.0

题目:POJ-2965

http://poj.org/problem?id=2965

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

Source

Northeastern Europe 2004, Western Subregion

解题思路:

DFS!

题目是跟着POJ很好很有层次感做的,也就是说做过了POJ-1753

基础框架来自之前AC的提交,改动不多,思路也完全相同,甚至更简单一点;

1753的题解:点击打开链接

改动地方在于Flip函数中翻转范围更大了,要注意的是在for循环过后要再反转一次中心点,否则中心点经过循环的两次反转相当与没有翻。

第二个难点就是注意step的保存,将随时改变的动态nstep最后要保存在step中


AC代码:

#include <iostream>
using namespace std;
#define MAX 10000000int step[20][2],nstep[20][2];
int map[5][5];
int ans= MAX;void Flip(int x,int y)	{for (int i=0;i<4;++i){map[x][i]=!map[x][i];map[i][y]=!map[i][y];}map[x][y]=!map[x][y];
}bool Judge(){for (int i=0;i<4;++i)	for (int j=0;j<4;++j)if(map[i][j]!=0)	return 0;return 1;
}void DFS(int x,int y,int t){if (Judge())  {step[t][0]=x+1;step[t][1]=y+1;if (t<ans)  {ans=t;for (int i=0;i<ans;++i){step[i][0]=nstep[i][0];step[i][1]=nstep[i][1];}}return;}if (x>=4||y>=4)		return;int nx,ny;nx=(x+1)%4;  ny=y+(x+1)/4;Flip(x,y);nstep[t][0]=x+1;nstep[t][1]=y+1;DFS(nx,ny,t+1);Flip(x,y);DFS(nx,ny,t); 	return ;
}int main()
{char in;for (int i=0;i<4;++i)  {for (int j=0;j<4;++j)	{cin>>in;if(in=='-')			map[i][j]=0;else if(in=='+')	map[i][j]=1;}}	DFS(0,0,0);cout<<ans<<endl;for (int i=0;i<ans;++i)	{cout<<step[i][0]<<" "<<step[i][1]<<endl;}return 0;
}