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 ...
随机推荐
- 数据库 oracle 函数
static OracleConnection mQracleConnecting = null; public static OracleConnection QracleConnecting { ...
- HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out的解决方法
问题描述: Pycharm创建Django项目提示:HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed o ...
- Sobel边缘检测算法
索贝尔算子(Sobel operator)主要用作边缘检测,在技术上,它是一离散性差分算子,用来运算图像亮度函数的灰度之近似值.在图像的任何一点使用此算子,将会产生对应的灰度矢量或是其法矢量 Sobe ...
- Iris_xorm
xorm表基本操作及高级操作 表结构基本操作 对表结构的操作最常见的操作是查询和统计相关的方法,我们首先来看相关实现: 条件查询 Id值查询:参数接收主键字段的值.例如: var user User ...
- sqlldr总结参数介绍
有效的关键字: userid -- ORACLE username/password control - 控制文件 log - 记录的日志文件 * 表示 ...
- form表单提交且接口回调显示提交成功
前端: <form method="post" enctype="multipart/form-data" id="formSubmit&quo ...
- threading 官方 线程对象和锁对象以及条件对象condition
官方地址:https://docs.python.org/2/library/threading.html#thread-objects 以下只截取condition部分,其他Lock()以及thre ...
- 【做题笔记】P1330 封锁阳光大学
读题易得:对于有边的两个点 \(u,v\) ,能且仅能其中一点对这条边进行封锁. 什么意思呢?假设给这张图上的点进行染色,那么对于上述的两个点 \(u,v\) ,\(u,v\) 必须异色(理解这一点很 ...
- Redis07——Redis五大数据类型 set
set Redis中set可以自动排重(不会出现重复数据),提供了判断某个成员是否在一个set集合内的重要接口(list没有此功能) 无序集合,底层是一个value为null的hash表,添加.删除. ...
- 第十篇 深入Python的dict和set(一)