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. jQuery设置全选和全反选

    HTML 代码: <input type="checkbox" id="allChecked" onclick="setAllChecked(t ...

  2. vue中 eCharts 自适应容器

    在 vue 脚手架开发中,echarts图表自适应容器的方法: 父组件: <template> <div class="statistics_wrap"> ...

  3. [原创]Debian9 安装配置MariaDB

    序言 这次玩次狠得.除了编译器使用yum安装,其他全部手动编译.哼~ 看似就Nginx.PHP.MySql三个东东,但是它们太尼玛依赖别人了. 没办法,想用它们就得老老实实给它们提供想要的东西. 首先 ...

  4. Spark job执行流程消息图

    Spark job执行流程消息图 1.介绍

  5. VOS3000设置落地网关优先级

    问题描述: 现在有两种套餐卡A:无限通话B:每月3000分钟两个落地网关:GW100:32线用的A套餐GW101:32线用的B套餐 比如现在是12月5号还没到月底,突然发现GW101上所剩可用通话时间 ...

  6. Ubuntu 如何将桌面上的Home中的文件夹除去

    安装Ubuntu后, 由于无法用Terminal(终端)进入带中文的文件夹,会引起很多操作不便.很多朋友想到了将它们都改成中文,但是当再次开机重启使却会发现,原本光洁的桌面现在竟然出现了一堆文件夹?? ...

  7. PHP @ at 记号的作用

    看PHP的代码,总有些行前边有@符号,一直不知道是什么意思. 例如dede5.7     @$ni=imagecreatetruecolor($ftoW,$ftoH); 今天用到了,就记一下吧.其实它 ...

  8. select_related()函数

    Django获取数据实体的时候,返回的对象一个实体或多个实体,也就是QuerySet,它是Django专有的东西,具体的理解,它是类似Python的字典的东西,但它并不实现字典的所有方法.今天讲解的是 ...

  9. framework7 v2.x轮播图写法:

    <div class="swiper-container swiper-init travel-index-swiper"> <div class="s ...

  10. 深入理解MyBatis-Spring中间件(spring/mybatis整合)

    转:http://blog.csdn.net/fqz_hacker/article/details/53485833 Mybatis-Spring 1.应用 mybatis是比较常用的数据库中间件,我 ...