POJ 2965 The Pilots Brothers' refrigerator (DFS)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 15136 | Accepted: 5660 | 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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue> using namespace std; char str[][]; int change[]={ , , ,, ,,,
,, ,,,, , , }; int vis[];
int path[]; struct node{
int status;
int step;
}; void BFS(int curstate){
queue<node> q;
while(!q.empty())
q.pop();
node cur,next;
cur.status=curstate, cur.step=;
vis[cur.status]=;
q.push(cur);
while(!q.empty()){
cur=q.front();
q.pop();
for(int i=;i<;i++){
next.status=cur.status^change[i];
next.step=cur.step+;
if(!vis[next.status]){
vis[next.status]=;
path[next.status]=i;
q.push(next);
}
if(next.status==){
printf("%d\n",next.step);
int k=next.status;
while(k!=curstate){
printf("%d %d\n",path[k]/+,path[k]%+);
k=k^change[path[k]];
}
return ;
}
}
}
}
/*
int num[16]; //即求change[]的方法 void init(){
for(int i=0;i<16;i++){
num[i]=0;
for(int j=0;j<16;j++)
if((i/4==j/4) || (i%4==j%4))
num[i]=(num[i]<<1)+1;
else
num[i]<<=1;
}
}
*/
int main(){ //freopen("input.txt","r",stdin);
/*
init();
for(int i=0;i<16;i++)
printf("%d ",num[i]);
printf("\n");
*/
while(~scanf("%s",str[]+)){
for(int i=;i<;i++)
scanf("%s",str[i]+);
int status=;
for(int i=;i<=;i++)
for(int j=;j<=;j++){
status<<=;
if(str[i][j]=='-')
status |= ;
}
if(status==){
printf("0\n");
continue;
}
memset(vis,,sizeof(vis));
BFS(status);
}
return ;
}
4 4
Source
POJ 2965 The Pilots Brothers' refrigerator (DFS)的更多相关文章
- POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
- POJ 2965 The Pilots Brothers' refrigerator (暴力枚举)
https://vjudge.net/problem/POJ-2965 与poj-1753相似,只不过这个要记录路径.poj-1753:https://www.cnblogs.com/fht-lito ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- The Pilots Brothers' refrigerator(dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19718 ...
- POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16868 ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...
随机推荐
- CVPR14 图像检索papers
CVPR14年关于图像检索方面的papers,汇总成一个list,方便阅读. 图像检索 Triangulation embedding and democratic aggregation for i ...
- 3D屏保程序:汉诺塔
学过程序的人一定记得汉诺塔.我们学的第一个程序是HelloWorld,而碰到的第一个坑就是汉诺塔,短短十几行代码,不知花费了多少时间精力去理解.我记得当年的开发环境还是蓝屏的,汉诺塔程序的输出还是一行 ...
- Multi-label && Multi-label classification
Multi-label classification with Keras In today’s blog post you learned how to perform multi-label cl ...
- C#线程同步与死锁Monitor
在上一讲介绍了使用lock来实现C#线程同步.实际上,这个lock是C#的一个障眼法,在C#编译器编译lock语句时,将其编译成了调用Monitor类.先看看下面的C#源代码: public stat ...
- Lambda表达式 简介 语法 示例 匿名内部类
在AS中使用 Lambda 表达式 Demo地址:https://github.com/baiqiantao/MultiTypeTest.git Gradle(Project级别)中添加classpa ...
- UVA 11573 - Ocean Currents(BFS+优先队列)
UVA 11573 - Ocean Currents 题目链接 题意:给定一个海面.数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量,每次询问给一个起点一个终点,问起点到终点耗费的最小能量 ...
- C++类型转换的经典例子
- java核心技术36讲
https://time.geekbang.org/column/intro/82?utm_source=website&utm_medium=infoq&utm_campaign=8 ...
- VM虚拟机如何和主机共享文件夹或文件
请一定要选中Map as a network drive in Windows guests,否则将无法查看共享.
- ZH奶酪:PHP如何判断提交表单中多个复选框是否选中?
1.name命名为数组,例如“select[]” 2.例如这样选: 3.在后台使用$_POST['select']得到数组 4.然后就可以看到得到的数组了 原文链接:http://www.zhihu. ...