【HDU3085】nightmare2 双向BFS
对于搜索树分支很多且有明确起点和终点的情况时,可以采用双向搜索来减小搜索树的大小。
对于双向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的更多相关文章
- HDU3085(双向BFS+曼哈顿距离)题解
		Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ... 
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
		题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ... 
- HDU3085 Nightmare Ⅱ (双向BFS)
		联赛前该练什么?DP,树型,状压当然是爆搜啦 双向BFS就是两个普通BFS通过一拼接函数联系,多多判断啦 #include <iostream> #include <cstdio&g ... 
- HDU3085(KB2-G 双向bfs)
		Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ... 
- POJ1915Knight Moves(单向BFS + 双向BFS)
		题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ... 
- HDU 3085 Nightmare II 双向bfs 难度:2
		http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ... 
- POJ 3170 Knights of Ni (暴力,双向BFS)
		题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ... 
- [转] 搜索之双向BFS
		转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ... 
- 双向BFS
		转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ... 
随机推荐
- layui表格和弹出框的简单示例
			<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ... 
- 自己动手写把”锁”---LockSupport深入浅出
			本篇是<自己动手写把"锁">系列技术铺垫的最后一个知识点.本篇主要讲解LockSupport工具类,它用来实现线程的挂起和唤醒. LockSupport是Java6引入 ... 
- YouTube视频下载的12个软件(Win和Mac)
			如今,观看视频已经成为人们生活中重要的一部分.很多时候,我们都需要用到视频,比如教育用途.会议报告.休闲娱乐以及广告宣传等.如果你觉得有时候资源不好找的话,不放去看下YouTube.YouTube是世 ... 
- ssh实现办公室电脑连接家中的电脑
			友情提示:如果您不知道您家路由器管理页面的密码,请您忽略此文. 问题背景: 家中有台笔记本电脑,它是通过家中的路由器与外界联网的,这时,我想通过ssh服务让公司的电脑能连上我家中的笔记本. 可以画个图 ... 
- Linux下批量ping某个网段ip的脚本
			比如现在需要对172.16.50.0/24网段的ip进行检查,检查哪些ip现在被占用,哪些ip没有被占用,可以通过ping命令来检查,脚本如下: [root@uatdns01 opt]# vim /o ... 
- Nginx+upstream针对后端服务器容错的运维笔记
			熟练掌握Nginx负载均衡的使用对运维人员来说是极其重要的!下面针对Nignx负载均衡upstream容错机制的使用做一梳理性说明: 一.nginx的upstream容错 1)nginx 判断节点失效 ... 
- 【CV】ICCV2015_Learning Temporal Embeddings for Complex Video Analysis
			Learning Temporal Embeddings for Complex Video Analysis Note here: it's a review note on novel work ... 
- ubuntu——caffe配置deeplab
			1. 下载deeplab 2. 安装matio sudo apt-get install libmatio-dev 3. 修改Makefile文件 LIBRARIES += glog gflags p ... 
- Nginx rewrite模块深入浅出详解
			rewrite模块(ngx_http_rewrite_module) nginx通过ngx_http_rewrite_module模块支持url重写.支持if条件判断,但不支持else.另外该模块需要 ... 
- Laravel 常见错误 1071 Specified key was too long
			Laravel 5.5 + Mysql 5.5 ,执行 migrate 时,提示索引长度超过指定的 1000 bytes 原因: Mysql 对索引有一定的长度限制,版本不同长度不同: MyIsAm ... 
