[题解](双向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 ...
随机推荐
- haproxysocket 参数记录
haproxy的一些指标 pxname 组名 svname 服务器名 qcur 当前队列 qmax 最大队列 scur当前会话用户 smax最大会话用户 slim会话限制 stot会话 ...
- 应用提交 App Store 上架被拒绝
审核的参考资料 https://developer.apple.com/icloud/documentation/data-storage/index.html https://developer.a ...
- 分享知识-快乐自己:搭建第一个 Hibernate (Demo)
使用 Hibernate 完成持久化操作 七大 步骤: 1.读取并解析配置文件及映射文件: Configuration configuration=new Configuration().config ...
- T57
“期待使我产生了介于幸福与恐惧之间的激动”The anticipation produced in me a sensation somewhat between bliss and fear他猛一下 ...
- 线程绑定CPU核-sched_setaffinity
CPU亲合力就是指在Linux系统中能够将一个或多个进程绑定到一个或多个处理器上运行. 一个进程的CPU亲合力掩码决定了该进程将在哪个或哪几个CPU上运行.在一个多处理器系统中,设置CPU亲合力的掩码 ...
- linux命令学习:echo详解,格式化输出,不换行输出
shell脚本不换行刷新数据 #!/bin/bash ] do a=$(ifconfig eth0 | grep 'RX pac' | awk '{print $2}' | awk -F: '{pri ...
- CardView以及RecycleView的一些问题
下面这些属性在listview的标签里有用,在recyclerView里没用. tools:listitem="@layout/list_single_answer_item_borrowe ...
- highcharts 图例全选按钮方法
$('#uncheckAll').click(function(){ var chart = $('#container').highcharts(); var series = chart.seri ...
- 数据库和ADO
数据库语言 数据库的简易流程(数据库客户端软件和数据库服务软件的执行流程) 主键的概念 如何创建主键 如何创建外键 主外键关系的概念以及使用 数据库的主要类型 数据库的主要数据类型 使用SQL语句来创 ...
- hive 面试题
使用 Hive或者自定义 MR 实现如下逻辑 product_no lac_id moment start_time user_id county_id staytime city_id 134291 ...