POJ 2965             The Pilots Brothers' refrigerator

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

代码是看过网上大佬题解的,因为不知道怎么记录路径...

#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x3f3f3f;
int ans,flag;
int map[][];
struct node{
int p,q;
}a[];
int judge(){
for(int i=;i<=;++i){
for(int j=;j<=;++j){
if(map[i][j]==){
return ;
}
}
}
return ;
}
void swap(int x,int y){
map[x][y]=!map[x][y];
for(int i=;i<=;++i)
{
map[x][i]=!map[x][i];
map[i][y]=!map[i][y]; //翻过来
}
}
void dfs(int x,int y,int step){
if(step==ans)
{
flag=judge(); //判断是否全翻完;
return;
}
if(flag||x>=)
{
return; // 判断是否走完或者翻完
}
swap(x,y);
if(y<){
dfs(x,y+,step+);
a[step].p=x;//记录路径
a[step].q=y;
} //全部16枚举一遍;
else{
dfs(x+,,step+);//换行翻
a[step].p=x;
a[step].q=y;
}
swap(x,y);
if(y<)
{
dfs(x,y+,step);
}
else
{
dfs(x+,,step);
}
}
int main(void){
char b;
for(int i=;i<=;++i){
for(int j=;j<=;++j){
scanf("%c",&b);
if(b=='-')
{
map[i][j]=;
}
else{
map[i][j]=;
}
}
getchar();
}
flag=;
for(int i=;i<=;++i){//判断i步是否可以达到目标
ans=i;
dfs(,,);
if(flag) //遍历
{
break;
}
}
if(flag) //如果能成功达到,就输出
{
printf("%d\n",ans);//需要几次
for(int i=;i<ans;++i){
printf("%d %d\n",a[i].p,a[i].q);
}
}
return ;
}

dfs+枚举,flip游戏的拓展POJ2965的更多相关文章

  1. poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

    Description Flip game squares. One side of each piece is white and the other one is black and each p ...

  2. POJ 1753 Flip Game DFS枚举

    看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...

  3. POJ 1753 Flip Game (DFS + 枚举)

    题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...

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

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

  5. POJ1288 Sly Number(高斯消元 dfs枚举)

    由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...

  6. POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)

    题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gc ...

  7. POJ 1270 Following Orders (拓扑排序,dfs枚举)

    题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y.    要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...

  8. HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)

    想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...

  9. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

随机推荐

  1. adb 连接手机

    adb kill-server adb start-server 可能会遇到问题华为手机: 有应用遮挡了权限请求界面,设置应用无法验证你的回应 系统导航关闭悬浮球 然后重启adb  server ad ...

  2. tensorflow学习笔记——GoogLeNet

    GoogLeNet是谷歌(Google)研究出来的深度网络结构,为什么不叫“GoogleNet”,而叫“GoogLeNet”,据说是为了向“LeNet”致敬,因此取名为“GoogLeNet”,所以我们 ...

  3. jvm(1):内存结构

    JVM内存结构 JVM内存的运行时数据区: 线程私有(在线程启动时创建) 程序计数器Program Counter Register 一块较小的内存空间,可以看作是当前线程所执行的字节码的行号指示器, ...

  4. 不可将布尔值直接与true或者1进行比较

    不可将布尔值直接与TRUR.FALSE或者"0"."1"进行比较. 根据布尔值的定义,零值为"假"(记为FALSE),任何非零值都是&quo ...

  5. 算法竞赛入门经典第二版 回文词P49

    #include<bits/stdc++.h> using namespace std; char rev[]="A 3 HIL JM O 2TUVWXY51SE Z 8 &qu ...

  6. rancher2中文文档地址

    rancher2中文文档地址 待办 https://docs.rancher.cn/

  7. Spark 中 GroupByKey 相对于 combineByKey, reduceByKey, foldByKey 的优缺点

    避免使用GroupByKey 我们看一下两种计算word counts 的方法,一个使用reduceByKey,另一个使用 groupByKey: val words = Array("on ...

  8. linux零碎001

    glibc:GNU发布的libc库,即c运行库.glibc是linux系统中最底层的api,几乎其它任何运行库都会依赖于glibc. uclibc:是一个面向嵌入式Linux系统的小型的C标准库.最初 ...

  9. centos7 命令 对比 cenots6 命令

    1)  列出所有service开机启动项 centos 7 systemctl list-unit-files |grep enabled centos 6 chkconfig --list|grep ...

  10. [RedHat]“is not in the sudoers file”解决方法

    当在终端执行sudo命令时,系统提示“luckchengis not in the sudoers file”: $ sudo ls Password: luckcheng is not in the ...