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 ...
随机推荐
- 【BZOJ2428】[HAOI2006]均分数据
Description 已知N个正整数:A1.A2.…….An .今要将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小.均方差公式如下: ,其中σ为均方差,是各组数据和的平均值,xi为第 ...
- linux whereis which
whereis 命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s). [root@localhost ~]# whereis svn svn ...
- ios App 加急审核
下面进入正题.提交完成后进入加急审核页面. 链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i wo ...
- 一种高斯模糊渐变动画的实现-b
关于高斯模糊的方式有很多种,但是如果需要模糊渐变,那么对这种高斯模糊算法的性能要求是比较高的,今天这里重点不讨论算法,只是提供一个动画实现的思路.动画效果如下: 高斯模糊渐变动画 //高斯模糊 -(U ...
- ios 8和iOS 9 适用的uidatepicker
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n ...
- 慎用ReentrantLock
前言: 代码简洁与性能高效无法两全其美,本文章专注于并发程序的性能,如果您追求代码简洁,本文章可能不太适合,本文章属于Java Concurrency in Practice读书笔记. 在java5中 ...
- mysql 权限管理
参考: http://www.cnblogs.com/Richardzhu/p/3318595.html 一.MySQL权限简介 关于mysql的权限简单的理解就是mysql允许你做你全力以内 ...
- oracle-number(5,2)
insert into emp values(70000.123); 只能存储 整数的前3位, 小数点后面的2位
- C++ static、const和static const 以及它们的初始化
转自C++ static.const和static const 以及它们的初始化 const定义的常量在超出其作用域之后其空间会被释放,而static定义的静态常量在函数执行后不会释放其存储空间. s ...
- MonoBehaviour的事件和具体功能总结
原地址:http://blog.csdn.net/dingxiaowei2013/article/details/26215577 苦于Visual Studio 2013没有对MonoBehavio ...