//题目: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. [ Web Service ] [ SOAP ] [ JSON ] [ XML ] 格式轉換

    JSON格式產生器_Demo JSON格式產生器_ObjGen - Live JSON Generator JSON格式整理_JSON Formatter & Validator Online ...

  2. PHP curl 模拟登录

    //提交数据,生成cookie,将cookie保存在临时目录下//在指定目录中建立一个具有唯一文件名的文件.如果该目录不存在,tempnam() 会在系统临时目录中生成一个文件,并返回其文件名 $co ...

  3. 特定用户QQ群聊天记录导出的实现

    一.把QQ群的聊天记录txt格式导出 消息管理器 -> 选择要导出的群 -> 右击.导出   这里要注意 : 导出之后的 文本是 unicode 编码的,需要转换 ==|| 之前不知道,搞 ...

  4. cc2640-各DEMO板性能分析

    一.测试方法: 将4种模块同时上电,测量每个模块达到的最远距离.以稳定能建立通讯为连接上依据.4种板子分析为 1号阿莫DEMO板,2号咱们自己DEMO板,3号嘉源电子DEMO,4号陆程电子DEMO 全 ...

  5. ffmpeg yuv转h264

    ffmpeg -s 176x144 -i  container_qcif_176_144.yuv -b:v 7776k -r 25 -vcodec libx264 ds.h264

  6. angularApi网站用vue重构

    最近在博客园上看到不少关于vue的文章但感觉都是在简单原生写法上,真正vue在实际开发中的优点组件化,spa应用,路由好像都没涉及到,我在学angular1的时候发现没有中文版的api,于是本人不才弄 ...

  7. 使用maven 命令运行项目

    安装好maven3 配置好环境变量后, 输入mvn -v 查看安装是否成功, 然后导入maven项目, 选择import 导入选择Exsting Maven Projects, 接下来就准备运行一下m ...

  8. javascript eval 执行过程

    当执行eval时,会执行如下过程 eval(x): 1.如果 x的类型不是string,那么会return x; 2.把x转换成 ecmascript 代码.如果转换失败,责抛出SyntaxError ...

  9. Qt多国语言

    项目中需要多语言的部分以tr宏包含 例:setWindowTitle(tr("编辑")); .pro项目文件加入CODECFORTR = utf-8 #or gbk#DEFAULT ...

  10. 选择排序O(n^2)与快速排序O(nlogn)的优越性代码体现

    随机函数生成一个超大数组: [code]: #include <iostream> #include <stdio.h> #include<time.h> #inc ...