对于搜索树分支很多且有明确起点和终点的情况时,可以采用双向搜索来减小搜索树的大小。

对于双向BFS来说,与单向最大的不同是双向BFS需要按层扩展,表示可能到达的区域。而单向BFS则是按照单个节点进行扩展,因为只有当前状态。

代码如下:

#include <bits/stdc++.h>
using namespace std;
const int maxn=810; char mp[maxn][maxn];
int n,m,tot,step,f;
struct node{
int x,y;
}boy,girl,ghost[2];
queue<node> q[2];//两个队列
bool vis[2][maxn][maxn];//两个表示可达性 void init(){
tot=0;
while(q[0].size())q[0].pop();
while(q[1].size())q[1].pop();
memset(vis,0,sizeof(vis));
} void read_and_parse(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",mp[i]+1);
for(int j=1;j<=m;j++){
if(mp[i][j]=='M')boy=(node){i,j};
if(mp[i][j]=='G')girl=(node){i,j};
if(mp[i][j]=='Z')ghost[tot++]=(node){i,j};
}
}
} bool right(int x,int y){
if(x<1||x>n||y<1||y>m||mp[x][y]=='X')return 0;
if(abs(x-ghost[0].x)+abs(y-ghost[0].y)<=step<<1)return 0;
if(abs(x-ghost[1].x)+abs(y-ghost[1].y)<=step<<1)return 0;
return 1;
} const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0}; bool bfs(int idx){
int size=q[idx].size();
while(size--){
node now=q[idx].front();q[idx].pop();
if(!right(now.x,now.y))continue;//查看当前状态是否合法 for(int i=0;i<4;i++){
int x=now.x+dx[i],y=now.y+dy[i];
if(!right(x,y)||vis[idx][x][y])continue;
if(vis[1-idx][x][y])return 1;
vis[idx][x][y]=1;
q[idx].push((node){x,y});
}
}
return 0;
} void solve(){
step=0,f=0;
q[0].push((node){boy.x,boy.y});
q[1].push((node){girl.x,girl.y}); while(q[0].size()||q[1].size()){
++step;
for(int i=1;i<=3;i++)if(bfs(0)){f=1;break;}//每次扩展三层,表示走三步可能到达的状态
if(bfs(1)){f=1;break;}
} if(f)printf("%d\n",step);
else puts("-1");
} int main(){
int T;scanf("%d",&T);
while(T--){
init();
read_and_parse();
solve();
}
return 0;
}

【HDU3085】nightmare2 双向BFS的更多相关文章

  1. HDU3085(双向BFS+曼哈顿距离)题解

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  3. HDU3085 Nightmare Ⅱ (双向BFS)

    联赛前该练什么?DP,树型,状压当然是爆搜啦 双向BFS就是两个普通BFS通过一拼接函数联系,多多判断啦 #include <iostream> #include <cstdio&g ...

  4. HDU3085(KB2-G 双向bfs)

    Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. POJ1915Knight Moves(单向BFS + 双向BFS)

    题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...

  6. HDU 3085 Nightmare II 双向bfs 难度:2

    http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...

  7. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  8. [转] 搜索之双向BFS

    转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...

  9. 双向BFS

    转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2)  快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...

随机推荐

  1. Error【0006】:could not create or update nagios.configtest

    1. 错误背景 在本系列博客<Nagios监控系统部署(源码).md>中(笔记内链:Nagios监控系统部署(源码).md,博客园地址:https://www.cnblogs.com/li ...

  2. C/C++中连接函数strcat的应用(简单讲解)

    有位学弟问到我如何将两个字符连接起来,想想java/python里面可以直接用+连接起来,可是C/C++里面有没有这么方便的做法呢? 答案是有的,在C语言的string.h库中有个神奇的函数叫做str ...

  3. Zookeeper 3.4.8分布式安装

    1.机器信息 五台centos 64位机器 2.集群规划 Server Name Hadoop Cluster Zookeeper   Ensemble HBase Cluster Hadoop01 ...

  4. junit-test

    一.题目简介: 用单元测试junit4测试calculator类的加减乘除四种方法,来初步学习junit4的学习方法. 二.源码的github链接  :https://github.com/weare ...

  5. There are no enabled repos.

    今天要记录一下自己懵逼的一天,原来自己是Ubuntu系统,还以为是centos,导致命令错了 There are no enabled repos. Run "yum repolist al ...

  6. Aop事务小结(事务管理器和自身构建)

    声明市事务是利用AOP来实现的. 1.采用事务管理器AOP: <!--3.配置事务切面:控制住连接池 --> <bean id="transactionManager&qu ...

  7. ABP集成短信发送模块

    ABPZero并没有手机短信发送功能,现在我们来集成一个,为后面注册.登录作铺垫. 阿里云短信服务 首先需要在阿里云开通短信服务,连接地址 开通后,在签名管理中添加一个签名 在模板管理中添加一个模板, ...

  8. Mac+Docker环境下xdebug的配置

    由于容器化的需要,前几天我本地也换成了docker环境.就研究了一下docker环境下phpstorm和xdebug的配置. http://www.mmfei.com/?p=453 这个博客给出了一个 ...

  9. IP工具类

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.servlet.http.HttpServletReques ...

  10. 组件vue传值

    <div id="app"> <hs :message="name"></hs> 用来接收值 </div> &l ...