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+枚举的更多相关文章

  1. poj 2965 The Pilots Brothers' refrigerator (dfs)

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

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

  3. POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)

    http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...

  4. POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

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

  5. 枚举 POJ 2965 The Pilots Brothers' refrigerator

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

  6. The Pilots Brothers' refrigerator(dfs)

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

  7. POJ 2965 The Pilots Brothers' refrigerator 位运算枚举

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

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

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

  9. POJ 2965 The Pilots Brothers' refrigerator (DFS)

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

随机推荐

  1. ACM_括号匹配

    括号匹配(栈) Time Limit: 2000/1000ms (Java/Others) Problem Description: 给一组包含[]()两种括号的序列,检查是否是合法的. 如:()[] ...

  2. 内联标签------------大多数XHTML可以表示为两种类型的标签:块标签(block tag)和内联标签(inline tag)

    内联标签 <em> 强调,大部分浏览器渲染为斜体. <strong> 强调,大部分浏览器渲染为黑体. <sub> 下标 <sup> 上标 内联标签通常用 ...

  3. [转]mysql的查询、子查询及连接查询

    转自:http://www.cnblogs.com/rollenholt/archive/2012/05/15/2502551.html 一.mysql查询的五种子句         where(条件 ...

  4. Kerberos 简介——教你做个好人

    文章导读: 对称加密 非对称加密 数字证书 Kerberos认证流程 Hadoop生态利用Kerberos认证机制来识别可靠的服务和节点,保障Hadoop集群的安全,那么Kerberos到底是什么?为 ...

  5. redis+mysql读写方案

    前言:在web服务端开发的过程中,redis+mysql是最常用的存储解决方案,mysql存储着所有的业务数据,根据业务规模会采用相应的分库分表.读写分离.主备容灾.数据库集群等手段.但是由于mysq ...

  6. LN : leetcode 241 Different Ways to Add Parentheses

    lc 241 Different Ways to Add Parentheses 241 Different Ways to Add Parentheses Given a string of num ...

  7. Objective-C设计模式——中介者Mediator(对象去耦)

    中介者模式 中介者模式很好的诠释了迪米特法则,任意两个不相关的对象之间如果需要关联,那么需要通过第三个类来进行.中介者就是把一组对象进行封装,屏蔽了类之间的交互细节,使不同的类直接不需要持有对方引用也 ...

  8. [ CodeForces 1059 C ] Sequence Transformation

    \(\\\) \(Description\) 你现在有大小为\(N\)的一个数集,数字分别为 \(1,2,3,...N\) ,进行\(N\)轮一下操作: 输出当前数集内所有数的\(GCD\) 从数集中 ...

  9. (8)string对象上的操作1

     读写操作 //读写string对象的测试.//本程序输入两string类,输出两string类. #include <iostream> #include <string> ...

  10. 如何取消android studio启动时自动打开上次关闭的项目

    Androidstudio默认每次android studio启动就会自动打开上次关闭的项目,如果想要取消并让它显示此界面 只需要