poj 2965 The Pilots Brothers' refrigerator (dfs)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 17450 | Accepted: 6600 | 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 iand 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
//164K 454MS C++ 1319B 2014-04-26 11:52:45
/* 题意:
和poj 1753 差不多,不过这里翻转和判断有点不一样,翻转是行列都要翻,判断是
要全1才行。 解法和poj1753几乎一样,就是要加个二维数组记录一下输出路径。 */
#include<stdio.h>
#include<string.h>
int g[][];
int root[][];
int flag;
inline int judge(int tg[][])
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(g[i][j]==) return ;
return ;
}
void flip(int x,int y)
{
for(int i=;i<=;i++){
g[x][i]^=;
g[i][y]^=;
}
g[x][y]^=;
}
void dfs(int x,int y,int cnt,int n)
{
if(cnt==n){
flag=judge(g);
return;
}
if(flag || y>) return;
root[cnt][]=x;
root[cnt][]=y;
flip(x,y);
if(x<) dfs(x+,y,cnt+,n);
else dfs(,y+,cnt+,n);
flip(x,y);
if(x<) dfs(x+,y,cnt,n);
else dfs(,y+,cnt,n);
}
int main(void)
{
char c[];
while(scanf("%s",c)!=EOF)
{
memset(g,,sizeof(g));
for(int i=;i<;i++) g[][i+]=c[i]=='-'?:;
for(int i=;i<;i++){
scanf("%s",c);
for(int j=;j<;j++)
g[i+][j+]=c[j]=='-'?:;
}
flag=;
int cnt=-;
for(int i=;i<=;i++){
dfs(,,,i);
if(flag){
cnt=i;break;
}
}
printf("%d\n",cnt);
for(int i=;i<cnt;i++)
printf("%d %d\n",root[i][],root[i][]);
}
return ;
} /* -+--
----
----
-+-- +---
----
----
---- -+++
+---
+---
+--- ----
----
----
---- ++++
++++
++++
++++ */
poj 2965 The Pilots Brothers' refrigerator (dfs)的更多相关文章
- 枚举 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: 15136 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- 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 ...
- POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】
题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...
- poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...
- POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
随机推荐
- 使用Python对MySQL数据库操作
本文介绍Python3使用PyMySQL连接数据库,并实现简单的增删改查. 什么是PyMySQL? PyMySQL是Python3.x版本中用于连接MySQL服务器的一个库,Python2.x中则使用 ...
- python运算符及优先级顺序
python语言是一门脚本语言,支持面向对象.面向过程编程,兼具编译性和解释性的动态语言,整理出学习过程中一些基本Python运算符和运算符的优先级顺序. 一.算术运算符 运算符 描述 + 加 - 两 ...
- thinkphp 下多图ajax上传图片
碰到一个项目,有一个比较繁琐的功能6个ajax上传,基本上每个上传逻辑多不一样,记录一下 thinkphp的view页面: id方便找到这个元素 name一定要加 [ ] <div class= ...
- MySQL Waiting for table metadata lock的解决方法
最近需要在某一个表中新增字段,使用Sequel Pro 或者Navicat工具都会出现程序没有反应,使用 show processlist 查看,满屏都是 Waiting for table meta ...
- MySQL数据库查看数据表占用空间大小和记录数
MySQL数据库中每个表占用的空间.表记录的行数的话,可以打开MySQL的 information_schema 数据库.在该库中有一个 TABLES 表,这个表主要字段分别是: TABLE_SCHE ...
- 终于搞定了cxgrid的多行表头(转终于搞定了cxgrid的多行表头 )
终于搞定了cxgrid的多行表头 转自:http://mycreature.blog.163.com/blog/static/556317200772524226400/ 这一周都在处理dbg ...
- PrestaShop 网站漏洞修复如何修复
PrestaShop网站的漏洞越来越多,该网站系统是很多外贸网站在使用的一个开源系统,从之前的1.0初始版本到现在的1.7版本,经历了多次的升级,系统使用的人也越来越多,国内使用该系统的外贸公司也很多 ...
- Java学习笔记十:Java的数组以及操作数组
Java的数组以及操作数组 一:什么是数组: 数组可以理解为是一个巨大的“盒子”,里面可以按顺序存放多个类型相同的数据,比如可以定义 int 型的数组 scores 存储 4 名学生的成绩 数组中的元 ...
- PHP json_decode返回null解析失败原因
在PHP5.4之前 json_decode函数有两个参数json_decode有两个参数,第一个是待解析的字符串,第二个是是否解析为Array json_decode要求的字符串比较严格:(1)使用U ...
- HyperLedger Fabric 1.4 关键技术(6.4)
本节介绍从最底层的账本开始,逐一讲解账本的结构和存储.智能合约的编写和部署.通道的操作.节点的背书和提交.排序的共识和客户端SDK的接口调用,与交易流程顺序相反,由里及表的说明Fabric最关键的技术 ...