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 ...
随机推荐
- MAC 10.10 开机登录无敌风火轮问题解决方式
查明是第三方输入法引起的问题,我用的是搜狗输入法.所以把搜狗卸载就好了.(注意是卸载,不是单纯的从输入源里移除) 操作的思路是,首先要进入计算机,才干进行操作. 办法是开机进入单机模式,删除苹果一个文 ...
- 数据库实例: STOREBOOK > 用户 > 编辑 用户: DBSNMP
ylbtech-Oracle:数据库实例: STOREBOOK > 用户 > 编辑 用户: DBSNMP 编辑 用户: DBSNMP 1. 一般信息返回顶部 1.1, 1.2, 2 ...
- 谈谈javascript的函数作用域
在一些类似c语言的编程语言中,花括号内的每一段代码都具有各自的作用域,而且变量在声明他们的代码段之外是不可见的,我们称为块级作用域(block scope),而javascript中没有块级作用域.取 ...
- AT&T汇编语言——简单实例及工具演示
今天就来用详细实例代码来运用一下昨天所说的仅仅个工具的使用方法吧 这几个实例基本的目的是来熟悉一下汇编相关工具的使用方法及应用一下昨天刚说的汇编程序模板. 我们用到的工具主要有as,ld,gcc,gd ...
- 关于PHP写的投票网站之刷票终结版
告诉大家一个坏消息,还是有刷票的行为,有图有真相: 倒数第三项是时间轴,倒数第二项是IP,倒数第一项是邮箱,你们要刷票,拦都拦不住呀呀呀呀呀呀!!!!! 看看这些时间的间隔,看看这些IP的地址,哎,再 ...
- Typescript declaration: Merge a class and an interface
参考: https://stackoverflow.com/questions/47670959/typescript-declaration-merge-a-class-and-an-interfa ...
- Discuz上传错误
换了服务器后,上传图片的时候,显示上传100%,然后报错:upload error: 500.怎么回事那? [解决方法] 原来是php上传文件的时候,会首先上传到一个临时目录.如果临时目录没有权限,就 ...
- rapidxml 解析修改内存的值
1.使用rapidxml解析的时候,也就是 调用xmlDoc.parse<0>(xmlContent),特别注意,rapidxml会修改内存的值,把右尖括号>修改为'\0',因此特别 ...
- 使用GCD创建单例
+ (KKTextHUB *)sharedTextHUB { static KKTextHUB *sharedHub = nil; static dispatch_once_t onceToken; ...
- 解决mysql下区分表名大小写的问题
MySQL在Linux下采用 rpm方式安装后默认是: 数据库名与表名\表的别名\变量名是严格区分大小写 1.用root帐号登录,/etc/ mysql/my.cnf中的[mysqld]后添加lowe ...