//题目: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+位运算)的更多相关文章

  1. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  2. POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】

    题目链接: http://poj.org/problem?id=1753 题意: 给定冰箱门的开关情况,改变一个门则其所在行列的门都会发生改变,求出改变门的最少操作使得最终所有门都是打开状态. 代码: ...

  3. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  4. POJ 2965 The Pilots Brothers' refrigerator 位运算枚举

      The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 151 ...

  5. POJ 2965 The Pilots Brothers' refrigerator 暴力 难度:1

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16868 ...

  6. POJ 2965 The Pilots Brothers' refrigerator (DFS)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15136 ...

  7. poj 2965 The Pilots Brothers' refrigerator (dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17450 ...

  8. POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)

    http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...

  9. 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 ...

随机推荐

  1. jQuery阻止冒泡和HTML默认操作

    1:jQuery是一个快捷简便的JavaScript框架,说道框架可以直接理解为就是对原来底层的东西进行了封装使得开发者能够利用这个框架快速开发. 2:在当今的各个浏览器中都支持事件的冒泡,所谓的冒泡 ...

  2. mac安装memcache

    1.wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz 2.tar zxvf memcache-2.2 ...

  3. 使用FileResult导出txtl数据文件

    public FileResult ExportMobileNoTxt(SearchClientModel model){ var sbTxt = new StringBuilder(); ; i & ...

  4. what is WLAN ? when and why we need use it ?

    无线局域网络(Wireless Local Area Networks: WLAN)是相当便利的数据传输系统,它利用射频(Radio Frequency: RF)的技术,取代旧式碍手碍脚的双绞铜线(C ...

  5. 【信息学奥赛一本通】第三部分_队列 ex2_3produce 产生数

    给出一个整数n(n<=2000)(代码可适用n<=10^31)和k个变换规则(k<=15). 规则:1.1个数字可以变换成另1个数字: 2.规则中右边的数字不能为零. BFS #in ...

  6. Web 高性能开发汇总

    1. Http服务器: 让Windows Server 2008+IIS 7+ASP.NET支持10万个同时请求 大规模网站架构实战之体系结构(一) 大规模网站架构之WEB加速器SQUID(二) ii ...

  7. 【 Quartz】使用 JobListener (任务监听器可实现) 我想在一个任务执行后在执行第二个任务怎么办呢

    http://liuzidong.iteye.com/blog/1147528 Quartz之JobExecutionException 博客分类: Java Quartz quartzjobexec ...

  8. 0ffice365 Calendar API

    Calendar REST API in Office 365 APIs Preview http://msdn.microsoft.com/EN-US/library/office/dn792114 ...

  9. having与where区别

    having后可以跟组函数如avg(sal)而where后不可以有, 如果条件不是必须使用组函数最好还是使用where

  10. hibernate动态创建数据库表名几种方式

    数据库中数据量很大, 但又不可以删除时同时又要优化程序检索数据时间. 答:方式有很多比如 创建数据库表分区,创建索引, 存储过程等; 我这里采用动态创建数据库表的方式. 完全可以在不创建表分区情况下实 ...