您的位置:首页 > 其它

poj2965(枚举||dfs)

2013-05-30 10:55 281 查看
http://poj.org/problem?id=2965

The Pilots Brothers'
refrigerator

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

题意:有个冰箱,上面有4*4的开关,'-'表示开'+'表示关,每次改变一个开关对应的所子行和列的所有开关都会改变,求最小的开关数使得冰箱打开(保证有一种情况)还要输出打开冰箱的步骤。。。这题与poj1753确实很相似
参考代码/article/1968853.html
diss里面还有更高效的算法http://poj.org/showmessage?message_id=156561暂时还不理解
#include<iostream>

using namespace std;

bool
lock[10][10]={false};

bool flag;

int step;

int
ri[16],cj[16];

bool
isopen(void)//判断是否

{

for(int
i=1;i<5;i++)

for(int
j=1;j<5;j++)

if(lock[i][j]!=true)

return false;

return true;

}

void flip(int
row,int
col)
//其实参考POJ1753的翻棋方法也是可以做出来的,但是会超时不通过

{
//超时的原因就是翻棋时有太多多余的操作

lock[row][col]=!lock[row][col];
//把每一行的开关都变!

for(int
i=1;i<=4;i++)
//

lock[i][col]=!lock[i][col];
//把 每有一列的开关都变化

for(int
j=1;j<=4;j++)

lock[row][j]=!lock[row][j];

return;

}

void dfs(int
row,int col,int
deep)

{

if(deep==step)

{

flag=isopen();


return;

}

if(flag||row==5)return;

flip(row,col);

ri[deep]=row;//保存路径

cj[deep]=col;

if(col<4)

dfs(row,col+1,deep+1);

else

dfs(row+1,1,deep+1);

flip(row,col);

if(col<4)

dfs(row,col+1,deep);

else


dfs(row+1,1,deep);

return;

}

int
main(void)

{

char temp;

int i,j;

for(i=1;i<=4;i++)

for(j=1;j<=4;j++)

{

cin>>temp;

if(temp=='-')

lock[i][j]=true;

}

for(step=0;step<=16;step++)

{

dfs(1,1,0);

if(flag)break;

}

cout<<step<<endl;

for(i=0;i<step;i++)

cout<<ri[i]<<'
'<<cj[i]<<endl;

return 0;

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