poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)
//题目:http://poj.org/problem?id=2965
//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因此考虑用位表示。可以改变任
意一个把手的位置,但同时改变其所在的行和列。求最小步骤.
//耗时 800MS
1 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue> using namespace std;
int id,vis[<<];
int a[<<],b[<<],before[<<];
int p[]={, , , , , , , , , ,
, , , , , };
struct node
{
int x,step;
}; void hui(int x)
{
if(x!=id)
{
hui(before[x]);
printf("%d %d\n",a[x],b[x]);
}
}; void bfs()
{
queue<node>q;
int i;
struct node cur,next;
next.step=; next.x=id;
q.push(next);
memset(vis,,sizeof(vis));
vis[next.x]=; while(!q.empty())
{
cur=q.front();
q.pop();
for(i=; i<; i++)
{
next.x=cur.x^p[i];
next.step=cur.step+; if(vis[next.x]==)
{
before[next.x]=cur.x;
a[next.x]=-(i/);
b[next.x]=-(i%); vis[next.x]=;
q.push(next);
if(next.x==)
{
printf("%d\n",next.step);
hui(next.x);
}
}
}
}
}; int main()
{
int i,j;
char c;
id=;
for(i=; i<; i++)
{
for(j=; j<; j++)
{
id<<=;
scanf("%c",&c);
if(c=='+')
id+=;
}
getchar();
}
bfs();
return ;
}
bfs用 before数组 来回溯改变的点的坐标。
不过耗时太多,这题用 dfs更好,因为要求输出翻转过程。
DFS可以看一下:http://blog.csdn.net/lyy289065406/article/details/6642597这个博客代码跑时较少
poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)的更多相关文章
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】
题目链接: http://poj.org/problem?id=1753 题意: 给定冰箱门的开关情况,改变一个门则其所在行列的门都会发生改变,求出改变门的最少操作使得最终所有门都是打开状态. 代码: ...
- 枚举 POJ 2965 The Pilots Brothers' refrigerator
题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...
- POJ 2965 The Pilots Brothers' refrigerator 位运算枚举
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 151 ...
- POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16868 ...
- POJ 2965 The Pilots Brothers' refrigerator (DFS)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15136 ...
- poj 2965 The Pilots Brothers' refrigerator (dfs)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17450 ...
- POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
- POJ - 2965 The Pilots Brothers' refrigerator(压位+bfs)
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to op ...
随机推荐
- liger grid loadData
function fn_Search() { var beginDt = $("#txtBegin").val(); var endDt = $("#txtEnd&quo ...
- Spark Streaming揭秘 Day13 数据安全容错(Driver篇)
Spark Streaming揭秘 Day13 数据安全容错(Driver篇) 书接上回,首先我们要考虑的是在Driver层面,有哪些东西需要维持状态,只有在需要维持状态的情况下才需要容错,总的来说, ...
- 《C和指针》 读书笔记 -- 第10章 结构和联合
1.聚合数据类型能够同时存储超过一个的单独数据,c提供了两种类型的聚合数据类型,数组和结构. 2.[1] struct SIMPLE { int a; }; struct SIMPLE x; [2] ...
- javac mac 终端乱码
java和javac在简体中文的Mac OSX的终端(Terminal.app)环境下,默认是以GBK编码的中文输出各种诸如语法错误,数组访问越界之类的信息.但是,Mac的终端的默认编码是UTF-8, ...
- stm32之ADC学习
1.stm32中采用的是逐次逼近型模拟数字方式,那么什么是逐次逼近呢? 逐次逼近的方式类似于二分法,以8位数据为例:当输入一个模拟量的时候,首先取这8位数的一半,即1000 0000,与模拟量比较,大 ...
- nodejs的模块系统(实例分析exprots和module.exprots)
前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...
- MySQL的复制原理及配置
MySQL 的数据库的高可用性的架构大概有以下几种:集群,读写分离,主备.而后面两种都是通过复制来实现的.下面将简单介绍复制的原理及配置,以及一些常见的问题. 一.复制的原理 MySQL 复制基于主服 ...
- MyEclipse新建Maven工程
1.File-->New-->Web Project 2.新建文件夹 需要新建4个源文件夹,分别是 src/main/java src/main/resources src/te ...
- MyEcpilise引入Maven项目目录不正常,无JRE,无Maven Dependencies
右键项目--> Maven4MyEclipse --> Update Project Configuration
- 2817 Tangent的愤怒 - Wikioi
题目描述 Description 如果机房马上要关门了,或者你急着要和MM约会,请直接跳到第六个自然段. 第二段:本题改编自Usaco Training 4.4.2... 第三段:本题加大了数据强度. ...