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 ...
随机推荐
- 什么是GFW
GFW(Great Firewall of China)中文名:中国国家防火墙,建立于1998年.我们平常所说的“被墙了”,是指网站内容或服务被防火墙屏蔽了.而“FQ”是指突破防火墙去浏览那些被屏蔽的 ...
- 10046 trace and sql
1. SQLT 下载 从metalink上下载SQLT工具,参考文档 (以下大部分(SQL可以在sqlt\utl 目录下找到)) 1.1 SQLT 安装 SQLT安装在自己的schema SQLT ...
- 每天学点Linux命令:倒叙打印文件第二行的前100个大写字母
sed -n | rev 处理第二行 grep:提取大写字母 o: 不显示非结果 tr:删除换行 Cut:截取1-100个字符 rev:逆序 断断续续搞了好长时间. ...
- Python之pandas数据加载、存储
Python之pandas数据加载.存储 0. 输入与输出大致可分为三类: 0.1 读取文本文件和其他更好效的磁盘存储格式 2.2 使用数据库中的数据 0.3 利用Web API操作网络资源 1. 读 ...
- JPEG图像压缩出现资源不足问题的解决
1,问题的提出 公司开发了一个图像压缩上传程序.采用Delphi语言实现.大致步骤如下: 1,上传前将文件打开装载到TJpegImage, 2,创建一个TBitmap组件,设置其大小,采用Stretc ...
- My-Eclipse 快捷键大全
Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+ ...
- SDK_按键消息的拦截
按键消息的拦截 拦截是为了在原有功能的基础上增加自己的操作 扩展:当按钮接收到点击消息的时候,响应的是WM_LBUTTONDOWN消息,按钮的会被这个消息打包成一个WM_COMMAND 消息发送给父窗 ...
- cstring 转string
(1)CString转换为string CString cs(_T("cs")); string s; s = (LPCSTR)(CStringA)(cs); (2)string转 ...
- 19MVC设计模式
MVC设计模式 MVC英文即Model-View-Controller, 即把一个应用的输入.处理.输出流程按照Model.View.Controller的方式进行分离,这样一个应用被分成三个层——模 ...
- JavaScript中的方法
JavaScript中的方法 在JavaScript中,可以通过对象来调用对应的方法.在JavaScript中,有三个重要的window对象方法:用于显示警告信息的alert.用于显示确认信息的con ...