[题解](双向bfs)hdu_3085_Nightmare Ⅱ
发现直接搜索比较麻烦,但是要同时两个人一起走容易想到双向bfs,比较普通,
在判断是否碰到ghost时只要比较两点的曼哈顿距离大小和step*2(即ghost扩散的距离)即可,仔细思考也是可以想到的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=;
struct node{
int x,y;
node(){}
node(int a,int b){
x=a,y=b;
}
}mon[];
queue<node>pq[];
int n,m;
char maze[maxn][maxn];
int used[][maxn][maxn];
int dx[]={,-,,};
int dy[]={,,,-};
int step;
int check(node a){
if(a.x< || a.y< || a.x>=n || a.y>=m)return ;
if(maze[a.x][a.y]=='X')return ;
if((abs(a.x-mon[].x)+abs(a.y-mon[].y))<=*step)return ;
if((abs(a.x-mon[].x)+abs(a.y-mon[].y))<=*step)return ;
return ;
}
int bfs(int w){
// while(!pq[w].empty()){
int sum = pq[w].size();//这里不能搜完
while (sum--){
int x=pq[w].front().x,y=pq[w].front().y;pq[w].pop();
if(!check(node(x,y)))continue;
for(int i=;i<;i++){
int xx=x+dx[i],yy=y+dy[i];
if(!check(node(xx,yy)))continue;
if(!used[w][xx][yy]){
if(used[w^][xx][yy]==)return ;
used[w][xx][yy]=;
pq[w].push(node(xx,yy));
}
}
}
return ;
}
int ax,ay,bx,by;
int solve(){
while(!pq[].empty())pq[].pop();
while(!pq[].empty())pq[].pop();
pq[].push(node(ax,ay));
pq[].push(node(bx,by));
memset(used,,sizeof(used));
used[][ax][ay]=used[][bx][by]=;
step=;
while(!pq[].empty() || !pq[].empty()){
step++;
if(bfs()==)return step;
if(bfs()==)return step;
if(bfs()==)return step;
if(bfs()==)return step;
}
return -;
}
int main(){int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int cnt=;
for(int i=;i<n;i++)scanf("%s",maze[i]);
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(maze[i][j]=='G')
bx=i,by=j;
else if(maze[i][j]=='M')
ax=i,ay=j;
else if(maze[i][j]=='Z')
mon[cnt].x=i,mon[cnt++].y=j;
}
}
printf("%d\n",solve());
} }
[题解](双向bfs)hdu_3085_Nightmare Ⅱ的更多相关文章
- HDU3085NightmareII题解--双向BFS
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3085 分析 大意就是一个男孩和一个女孩在网格里,同时还有两个鬼,男孩每轮走三步,女孩每轮走一步,与鬼曼 ...
- HDU3085(双向BFS+曼哈顿距离)题解
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 6171 Admiral(双向BFS+队列)题解
思路: 最大步骤有20,直接BFS会超时. 因为知道开始情况和结果所以可以用双向BFS,每个BFS规定最大步骤为10,这样相加肯定小于20.这里要保存每个状态搜索到的最小步骤,用Hash储存.当发现现 ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- UVA1601-The Morning after Halloween(双向BFS)
Problem UVA1601-The Morning after Halloween Accept: 289 Submit: 3136 Time Limit: 12000 mSec Problem ...
- 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路
题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...
- HDU——1195Open the Lock(双向BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...
- CSUOJ2031-Barareh on Fire(双向BFS)
Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...
随机推荐
- Hadoop HA- zookeeper安装配置
安装集群 1.1 虚拟机: 3台安装好JDK的centos Linux虚拟机 1.2 安装包: 把下载好的zookeeper安装包,官网:http://mirror.bit.edu.cn/apache ...
- android程序的真正入口
代码出自MtAndroid 3.1.2完全开发手册,适用于Android平台. 概述 android程序的真正入口是Application类的onCreate方法.它的继承关系如下所示: java.l ...
- 夏日户外风景PSD素材
夏日户外风景PSD素材适用于向日葵素材背景图设计 地址:http://www.huiyi8.com/xiangrikui/
- Zookeeper用来干什么?
在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...
- super.onCreate(savedInstanceState) 以及onCreate(Bundle savedInstanceState, PersistableBundle persistentState)
super.onCreate(savedInstanceState) 调用父类的onCreate构造函数. 当一个Activity在生命周期结束前,会调用onSaveInsanceState()这个回 ...
- python之系统编程 --线程
###########使用线程完成多任务################ from threading import Thread import time #1. 如果多个线程执行的都是同一个函数的话 ...
- mac hosts
1 在命令行中输入:sudo vim /etc/hosts. 2 输入开机密码,就可打开文件.按下键盘i,对文件进入可编辑状态. 3 修改完,先按esc退出编辑模式,之后,按shift+:,再按wq来 ...
- 用PCA降维 (Principal Component Analysis,主成分分析)
参考资料:Mastering Machine Learning With scikit-learn 降维致力于解决三类问题.第一,降维可以缓解维度灾难问题.第二,降维可以在压缩数据的同时让信息损失最 ...
- python基础知识-数字
数字分为: 整数(int) 长整型(long) 浮点型(float) 一,整数 整数(int):即不带小数点的数字,如 12 ,45 ,0 ,3 #!/usr/bin/env python class ...
- Hibernate区分不同对象的方法
1.关系数据库按主键区分不同记录. create table CUSTOMERS (ID int promary key not null, NAME varchar(15)); insert ...