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. Python 分发包中添加额外文件【新手必学】

      在制作一个 Python 分发包时经常需要把一些文件添加到包中.最常见的例子是你希望通过 pip install命令安装 Python 包时会在 /etc/ 等目录下自动添加默认配置文件,由此可以 ...

  2. 关于memset....我太难了

    众所周知memset是个清空数组的好东西 然而...它慢的要死 直接让我从30ms炸到1045ms 于是快乐tle .... 是我的错 所以以后还是手动清空 (我真快乐)

  3. spring(四):IoC

    IoC-Inversion of Control,即控制反转 IoC意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制. 理解IoC的关键:"谁控制谁,控制什么,为何是反 ...

  4. Virtual Judge HDU 1241 Oil Deposits

    八方向   深搜 #include <iostream> #include<cstdio> #include<cstdlib> #include<algori ...

  5. ES6新的数据类型 generato,在AJAX中的应用

    1.next()方法会执行generator的代码,然后,每次遇到yield x;就返回一个对象{value: x, done: true/false},然后“暂停”.返回的value就是yield的 ...

  6. 使用VS2017开发安卓app(1)环境搭建

    本人新手,边学习边写笔记,有错误不足之处,望各位博友指正~ 想要用vs开发安卓app,需要在安装时勾选 Xamarin是一个跨平台开发框架.在这一框架内,开发iOS.Android.Windows P ...

  7. 励志写一篇有味道的博文------json

    之前有更古老的数据交互中间键xml,但是由于比较复杂后来出现了json json :轻量级数据交换格式 json与python数据对比 json        python object      d ...

  8. 【做题笔记】洛谷P1002过河卒

    虽说是 dp 入门题,但还是有很多细节需要注意 如果设 \(f_{x,y}\) 为目标地点为 \((x,y)\) 时走的种数,那么答案就是 \(f_{n,m}\) 在不考虑那只讨厌的马的情况下,对于任 ...

  9. AS常用快捷键

    Alt+回车 导入包,自动修正 Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt+Insert 生成代码(如g ...

  10. php 填写pdf 表单

    最近接到新的任务,要求把pdf的文档,编辑后发邮件 首先pdf表单提交,需要用到这个东西pdftk,GitHub地址:https://github.com/mikehaertl/php-pdftk 首 ...