您的位置:首页 > 其它

POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

2016-01-28 18:46 344 查看
The Pilots Brothers' refrigerator

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

同1753一样的代码,但是这题有一点不是很明白,就是没有Impossible的可能,


#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int handle[10][10];
int flag,step;
int r[20],c[20];
int all_open()
{
for(int i = 1; i <= 4; i++)
{
for(int j = 1; j <= 4; j++)
if(!handle[i][j])
return false;
}
return true;
}
void change(int row, int col)
{
handle[row][col] = !handle[row][col];  //没写这个DFS里面就是死循环了
for(int i = 1; i <= 4; i++)
{
handle[row][i] = !handle[row][i];
handle[i][col] = !handle[i][col];
}
}
void dfs(int row, int col, int deep)
{
if(deep == step)
{
flag = all_open();
return;
}
if(flag || row > 4)
return;

change(row, col);
r[deep] = row;
c[deep] = col;
if(col < 4)
{
dfs(row, col + 1, deep + 1);
}
else
{
dfs(row + 1, 1, deep + 1);
}
change(row, col);
if(col < 4)
{
dfs(row, col + 1, deep);
}
else
{
dfs(row + 1, 1, deep);
}
return;
}
int main()
{
char s[10];
while(scanf("%s", s) != EOF)
{
memset(handle, 0, sizeof(handle));
for(int i = 0; i < 4; i++)
if(s[i] == '-')
handle[1][i + 1] = 1;
for(int i = 2; i <= 4; i++)
{
scanf("%s", s);
for(int j = 0; j < 4; j++)
if(s[j] == '-')
handle[i][j + 1] = 1;
}

flag = 0;
for(step = 0; step <= 16; step++)
{
dfs(1, 1, 0);
if(flag)
break;
}
if(flag)
{
printf("%d\n", step);
for(int i = 0; i < step; i++)
printf("%d %d\n", r[i],c[i]);
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: