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. Allegro 反射仿真--IBIS模型转化

    一.IBIS模型的获取 a) 直接找芯片供应商 b) 从网上下载 i.到Google网站直接搜索某个型号的IBIS模型: ii. 到器件厂商的官方网站下载: iii.从专门提供IBIS模型的网站搜索下 ...

  2. python3练习100题——027

    又是一道迭代的题,没做好. 看了答案才试着写出来. 我一定要加油啊,为了尽快摆脱现在讨厌的生活! 原题链接:http://www.runoob.com/python/python-exercise-e ...

  3. 爬虫实战 豆瓣音乐top250 xpath

    刷知乎时刷到一篇爬取豆瓣音乐top250的,然后看了看,感觉自己的爬虫又更上一层楼了哈啊哈哈,尤其是发现xpath这么好用的东西. 不过也有一个感慨,就是有很多种方式都可以获得想要的数据,对于入门的新 ...

  4. C#源码转PlantUml

    1.下载编译开源工程PlantUmlClassDiagramGenerator 2.使用PlantUmlClassDiagramGenerator生成PlantUml文件 3.配置Vscode的Pla ...

  5. 随缘记录 LeetCode第168场周赛 2019-12-22

    5292. 划分数组为连续数字的集合 给你一个整数数组 nums 和一个正整数 k,请你判断是否可以把这个数组划分成一些由 k 个连续数字组成的集合. 如果可以,请返回 True:否则,返回 Fals ...

  6. Scale9Sprite不要在初始化的时候setCapInsets

    let scale9Sprite = new ccui.Scale9Sprite(filePath);scale9Sprite.setCapInsets(cc.rect(x,y,w,h)); 而非 l ...

  7. 等差数列Arithmetic Progressions题解(USACO1.4)

    Arithmetic Progressions USACO1.4 An arithmetic progression is a sequence of the form a, a+b, a+2b, . ...

  8. 如何预测股票分析--长短期记忆网络(LSTM)

    在上一篇中,我们回顾了先知的方法,但是在这个案例中表现也不是特别突出,今天介绍的是著名的l s t m算法,在时间序列中解决了传统r n n算法梯度消失问题的的它这一次还会有令人杰出的表现吗? 长短期 ...

  9. webpack: webpack简单打包后的代码(1)

    源码 本文研究的源码地址为:https://github.com/collect-webpack/practice/tree/master/webpack-01 在本研究的前提是 entry 的配置为 ...

  10. c++面向对象 之 基础 类修饰符 构造函数 友元函数

    1,类和对象 定义一个类,本质上是定义一个数据类型的蓝图.这实际上并没有定义任何数据,但它定义了类的名称意味着什么,也就是说,它定义了类的对象包括了什么,以及可以在这个对象上执行哪些操作. 类定义格式 ...