poj2965(枚举||dfs)
2013-05-30 10:55
281 查看
http://poj.org/problem?id=2965
The Pilots Brothers'
refrigerator
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
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;
}
The Pilots Brothers'
refrigerator
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 12574 | Accepted: 4703 | Special Judge |
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;
}
相关文章推荐
- poj 2965 枚举加dfs
- POJ 2965 The Pilots Brothers' refrigerator(dfs+枚举 || 规律)
- ACM--枚举+DFS/巧妙思想--POJ--2965--The Pilots Brothers' refrigerator--初期算法
- poj 2965 dfs+枚举
- (poj1.1.2)2965(The Pilots Brothers' refrigerator——DFS+枚举)
- POJ2965——DFS+枚举——The Pilots Brothers' refrigerator
- poj 1753||poj 2965 枚举+dfs
- POJ2965 状态压缩+BFS,DFS枚举,以及大牛的解法~
- poj2965(dfs暴力枚举)
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
- POJ 2965 The Pilots Brothers' refrigerator 枚举dfs
- POJ 2965(DFS+枚举)
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
- poj 2965 枚举+DFS
- poj 2965 The Pilots Brothers' refrigerator——DFS(分类是枚举)
- poj2965 The Pilots Brothers' refrigerator(直接计算或枚举Enum+dfs)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+dfs)
- POJ 2965 The Pilots Brothers' refrigerator(枚举+DFS)
- poj 2965 The Pilots Brothers' refrigerator 【dfs+枚举】【双十一大礼包】【刷题计划】
- 文章标题 POJ 2965 : The Pilots Brothers' refrigerator(枚举+dfs)