dfs+枚举,flip游戏的拓展POJ2965
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的更多相关文章
- 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 ...
- POJ 1753 Flip Game DFS枚举
看题传送门:http://poj.org/problem?id=1753 DFS枚举的应用. 基本上是参考大神的.... 学习学习.. #include<cstdio> #include& ...
- POJ 1753 Flip Game (DFS + 枚举)
题目:http://poj.org/problem?id=1753 这个题在開始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每一个棋子翻一遍.然后顺利的过了.所以也就没有深究. 省赛前 ...
- poj 2965 The Pilots Brothers' refrigerator(dfs 枚举 +打印路径)
链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...
- POJ1288 Sly Number(高斯消元 dfs枚举)
由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...
- 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 ...
- POJ 1270 Following Orders (拓扑排序,dfs枚举)
题意:每组数据给出两行,第一行给出变量,第二行给出约束关系,每个约束包含两个变量x,y,表示x<y. 要求:当x<y时,x排在y前面.让你输出所有满足该约束的有序集. 思路:用拓扑排 ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- 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 ...
随机推荐
- Centos7添加软链接
1.pycharm添加软连接: 命令行模式中输入命令: ln -s /root/pycharm-2018.1/bin/pycharm.sh /usr/bin/pycharm ps:代码中/root/p ...
- PP: Composite visual mapping for time series visualization
However: The conventional visual mapping maps each data attribute onto a single visual channel Purpo ...
- codeforces 1288C. Two Arrays(dp)
链接:https://codeforces.com/contest/1288/problem/C C. Two Arrays 题意:给定一个数n和一个数m,让构建两个数组a和b满足条件,1.数组中所有 ...
- ts中接口的用法
ts中的接口主要的作用是: 对“对象”进行约束描述 对“类”的一部分行为进行抽象 一.属性接口 接口中可定义 确定属性.可选属性.任意属性.只读属性 1.确定属性 interface UserInfo ...
- HTML链接标签
<a>超链接标签:常用属性: href:指定地址,要有HTTP协议.如果是本网站的html文件可以写路径 target:以什么方式打开 _self:在当前窗口打开(默认) _blank:新 ...
- Quartus ii 初学遇到的问题以及解决
第一次下载和运行Quartus后,发现几个问题: 下载时安装易出现问题. 解决:在官网下载后,在开始破解: 运行程序出现警告:RTL波形时出现问题? 解决:testbench程序出错. 问题3:在视频 ...
- 【sql】sql必知必会_02
chapter10 - 常用的sql标准有哪些,在SQL92中是如何使用连接的? sql两个主要的标准sql92.sql95: sql92中的五种连接方式: a.笛卡尔积:是一个数学运算,两个集合X和 ...
- Java多线程wait和notify协作,按序打印abc
有一个经典的多线程面试题:启三个线程,按序打印ABC 上代码: package cn.javaBase.study_thread1; class MyRunnable1 implements Runn ...
- 开发笔记—钉钉服务商应用isv开发,从应用配置,到获取客户企业通讯录
以第三方企业微应用为例 在第三方企业微应用应用时,比较底层的需求,就是应用需要获取客户企业的通讯录,即部门/员工的数据.本人整理以下几个关键数据,供大家开发参考. 新建第三方微应用时,能拿到这些初始数 ...
- 文件上传plupload组件使用
这段时间一直在使用文件上传,简要的介绍一下文件上传的组件使用,先上一段代码. var uploader = new plupload.Uploader( { //用来指定上传方式,指定多个上传方式请使 ...