The Pilots Brothers' refrigerator DFS+枚举
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
#include<cstdio>
#include<set>
#include<map>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#include<string>
using namespace std;
typedef long long LL; /*
DFS 枚举可能的操作步数
*/
int g[][],n,m,step;
int r[],c[];
bool f = false;
void change(int x,int y)
{
for(int i=;i<=;i++)
if(i!=x)
g[i][y] = -g[i][y];
for(int j=;j<=;j++)
if(j!=y)
g[x][j] = -g[x][j];
g[x][y] = -g[x][y];
}
bool judge()
{
for(int i=;i<=;i++)
for(int j=;j<=;j++)
if(g[i][j]==)
return false;
return true;
}
void dfs(int x,int y,int d)
{
if(d==step)//达到确定的查找步数
{
f = judge();
return ;
}
if(f||x>) return ;//越界或者已经找到解
change(x,y);
r[d]=x;//保存路径。
c[d]=y;//保存路径。
if(y<)
dfs(x,y+,d+);
else
dfs(x+,,d+);
change(x,y);//第二次将矩阵恢复,回溯法
if(y<)
dfs(x,y+,d);//这里由于反转两次该点,相当于没有取该点
else
dfs(x+,,d);
}
int main()
{
string str;
for(int i=;i<;i++)
{
cin>>str;
for(int j=;j<;j++)
{
g[i+][j+] = (str[j]=='-')?:;
}
}
for(step = ;step<=;step++)
{
dfs(,,);
if(f)
break;
}
printf("%d\n",step);
for(int i=;i<step;i++)
printf("%d %d\n",r[i],c[i]); }
The Pilots Brothers' refrigerator 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【枚举+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 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
- POJ2965The Pilots Brothers' refrigerator(枚举+DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22057 ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- The Pilots Brothers' refrigerator(dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19718 ...
- 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 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15136 ...
随机推荐
- C语言小项目-火车票订票系统
list.h #ifndef __LIST_H__ #define __LIST_H__ #include "stdafx.h" #include <stdio.h> ...
- C#将类对象转换为字典
主要是实现将类里面 的属性和对应的值转换为字典的键和值. public class RDfsedfw { /// <summary> /// 将匿名类转换为字典 /// </summ ...
- Git学习笔记(2)-创建仓库
一.Git简介 1.Git是什么 Git是分布式版本控制系统 2.Git有什么特点 (1)Git是分布式的SCM,SVN是集中式的 (2)Git每个历史版本存储完整的文件,SVN存储文件差异 (3)G ...
- switch-case用法
1.switch-case 一般的用它来做值匹配的. //匹配 就是全等. /* 语法: switch(表达式){ case 值1: 表达式的值和 值1匹配上了,需要执行的代码; break; cas ...
- UVM基础之------uvm_port_base
Port Base Classes uvm_port_component_base This class defines an interface for obtaining a port ...
- mongo 3.4分片集群系列之八:分片管理
这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...
- 使用Spring框架的步骤
“好记性,不如烂笔头”.今天正式接触了Spring框架,第一次接触Spring框架感觉Spring框架简化了好多程序代码,开发效率大大提高.现在介绍使用Spring框架的步骤.(使用spring-fr ...
- Change the color of a link in an NSMutableAttributedString
Swift Updated for Swift 3 Use with a textView.linkTextAttributes = [NSForegroundColorAttributeName: ...
- JMeter在linux上分布式压测遇到的坑(三)
master和slave机要在同一网段内,才能做分布式(Jmeter要配环境变量,这样不用手动起server) 分布式不成功,解决方案: 1.master端和slave端要ping通 2.ping通后 ...
- DropDownList 递归绑定分子公司信息
/// <summary> /// 绑定下拉框 /// </summary> /// <param name="ddl">绑定控件名称</ ...