The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25343   Accepted: 9786   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
4 4

思路:因为每个位置最多翻一次,所以最多翻16次,用枚举来做。(代码是看别人的,有一部分还不能完全理解。。)

#include "cstdio"
#include "algorithm"
#include "cstring"
char map[][];
int m[][];
typedef struct {
int s,t;
}road;
road a[];
int k,ans;
int P(){
for(int i=;i<=;i++){
for(int j=;j<=;j++){
if(m[i][j]==){
return ;
}
}
}
return ;
}
void G(int x,int y){
m[x][y]^=;
for(int i=;i<=;i++){
m[x][i]^=;
}
for(int i=;i<=;i++){
m[i][y]^=;
}
}
void dfs(int x,int y,int step) {
// G(x, y);
if (step == ans) {
k = P();//
return;
}
if(k||x>=){//不太理解这里为什么要判断k是否为1,个人觉得只要当ans=step时在dfs外判断即可??
return;
}
G(x, y);
if(y<){
dfs(x,y+,step+);
a[step].s=x;
a[step].t=y;
}
else {
dfs(x+,,step+);
a[step].s=x;
a[step].t=y;
}
G(x,y);
if(y<){
dfs(x,y+,step);
}
else {
dfs(x+,,step);
} }
int main(){
k=;
for(int i=;i<=;i++){
scanf("%s",map[i]);
for(int j=;j<;j++){
if(map[i][j]=='-'){
m[i][j+]=;
}
else if(map[i][j]=='+'){
m[i][j+]=;
}
}
}
for(int i=;i<=;i++){
ans=i;
dfs(,,);
if(k){
printf("%d\n",ans);
break;
}
}
for(int i=;i<ans;i++){
printf("%d %d\n",a[i].s,a[i].t);
}
return ;
}

poj 2965 枚举+DFS的更多相关文章

  1. POJ 1753 (枚举+DFS)

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40632   Accepted: 17647 Descr ...

  2. POJ 3050 枚举+dfs+set判重

    思路: 枚举+搜一下+判个重 ==AC //By SiriusRen #include <set> #include <cstdio> using namespace std; ...

  3. 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 ...

  4. poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

    链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...

  5. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  6. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  7. POJ 2965:The Pilots Brothers&#39; refrigerator

    id=2965">The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  8. POJ.3172 Scales (DFS)

    POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...

  9. poj 3740 Easy Finding 二进制压缩枚举dfs 与 DLX模板详细解析

    题目链接:http://poj.org/problem?id=3740 题意: 是否从0,1矩阵中选出若干行,使得新的矩阵每一列有且仅有一个1? 原矩阵N*M $ 1<= N <= 16 ...

随机推荐

  1. NIM(1) 一排石头的游戏

    最近在实习面试过程中,一个朋友遇到了该问题,从简单到复杂的思路如下,希望能给遇到相同问题的朋友一些启发和帮助.(内容来源网络和<编程之美>) 1.问题1 100个苹果 桌上有100个苹果, ...

  2. Android大牛

    张鸿洋 http://blog.csdn.net/lmj623565791/article/category/2680597 CSDN 鸿洋:http://blog.csdn.net/lmj62356 ...

  3. Python开发环境Wing IDE如何使用GTK和PyGObject

    Wing IDE是一个集成开发环境,可用于编辑.测试和调试使用PyGObject为GTK编写的Python代码.Wing IDE提供自动完成.调用提示.一个强大的调试器,以及许多其他功能,可帮助用户编 ...

  4. 【起航计划 030】2015 起航计划 Android APIDemo的魔鬼步伐 29 App->Preferences->Preferences from code

    这里我们使用类比的方法,将 PreferenceActivity 与一般的Activity 作个类比,可以更好的理解Android.Preference中的各个类. PreferenceActivit ...

  5. Mac 下显示隐藏文件或文件夹

    Mac 操作系统 隐藏显示文件 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:defaults write co ...

  6. Struts2_总结

    还未学习的内容,如果到时候要用到,再去学.1.Lamda 表达式(很复杂,很少用)2.验证框架(默认验证方法 validation.方法开始前验证.开始后验证)3.UI标签(用的不多)4.类型转换中的 ...

  7. ZIP文件流压缩和解压

    前面写了一篇文章 "ZIP文件压缩和解压", 介绍了" SharpZipLib.Zip " 的使用, 最近的项目中,在使用的过程中, 遇到一些问题. 比如, 现 ...

  8. VirtualBox中linux虚拟机和主机间的共享文件设置

    设置共享文件路径 点击虚拟机 设置-->选择 共享文件夹 (图1 设置共享文件夹) 设置共享文件夹路径 1 选择路径 2 填写自定义的共享名称(在后面需要与挂载路径相对应) 3 设置自动挂载/固 ...

  9. WannaCry勒索病毒卷土重来:日本本田工厂被迫关闭

    6月22日消息,前阵子WannaCry勒索病毒席卷全球,世界各地网络遭到攻击.日前,偃旗息鼓了一阵的WannaCry勒索病毒又重回人们视线,迫使一家汽车厂在日本关闭. 路透社报道,本田Sayama工厂 ...

  10. GNU Nano编辑器

    ^表示 control建 ^X 退出 nano ^O 保存文件 ^R 插入其他文件内容至光标位置 ^W 查找字符串 ^Y 跳至前一屏 ^V 跳至后一屏 ^K 剪切光标所在行并保存到剪贴板,或剪切选中内 ...