POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22057   Accepted: 8521   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 同1753一样的代码,但是这题有一点不是很明白,就是没有Impossible的可能,
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int handle[][];
int flag,step;
int r[],c[];
int all_open()
{
for(int i = ; i <= ; i++)
{
for(int j = ; j <= ; 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 = ; i <= ; 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 > )
return; change(row, col);
r[deep] = row;
c[deep] = col;
if(col < )
{
dfs(row, col + , deep + );
}
else
{
dfs(row + , , deep + );
}
change(row, col);
if(col < )
{
dfs(row, col + , deep);
}
else
{
dfs(row + , , deep);
}
return;
}
int main()
{
char s[];
while(scanf("%s", s) != EOF)
{
memset(handle, , sizeof(handle));
for(int i = ; i < ; i++)
if(s[i] == '-')
handle[][i + ] = ;
for(int i = ; i <= ; i++)
{
scanf("%s", s);
for(int j = ; j < ; j++)
if(s[j] == '-')
handle[i][j + ] = ;
} flag = ;
for(step = ; step <= ; step++)
{
dfs(, , );
if(flag)
break;
}
if(flag)
{
printf("%d\n", step);
for(int i = ; i < step; i++)
printf("%d %d\n", r[i],c[i]);
}
}
return ;
}
上一篇:使用反射动态调用ActiveX控件


下一篇:struts2(三)之表单参数自动封装与参数类型自动转换