Nightmare Ⅱ HDU - 3085 (双向bfs)
You may suppose that little erriyue and his girl friend can move in 4 directions. In each second, little erriyue can move 3 steps and his girl friend can move 1 step. The ghosts are evil, every second they will divide into several parts to occupy the grids within 2 steps to them until they occupy the whole maze. You can suppose that at every second the ghosts divide firstly then the little erriyue and his girl friend start to move, and if little erriyue or his girl friend arrive at a grid with a ghost, they will die.
Note: the new ghosts also can devide as the original ghost.
InputThe input starts with an integer T, means the number of test cases.
Each test case starts with a line contains two integers n and m, means the size of the maze. (1<n, m<800)
The next n lines describe the maze. Each line contains m characters. The characters may be:
‘.’ denotes an empty place, all can walk on.
‘X’ denotes a wall, only people can’t walk on.
‘M’ denotes little erriyue
‘G’ denotes the girl friend.
‘Z’ denotes the ghosts.
It is guaranteed that will contain exactly one letter M, one letter G and two letters Z.
OutputOutput a single integer S in one line, denotes erriyue and his girlfriend will meet in the minimum time S if they can meet successfully, or output -1 denotes they failed to meet.Sample Input
3
5 6
XXXXXX
XZ..ZX
XXXXXX
M.G...
......
5 6
XXXXXX
XZZ..X
XXXXXX
M.....
..G... 10 10
..........
..X.......
..M.X...X.
X.........
.X..X.X.X.
.........X
..XX....X.
X....G...X
...ZX.X...
...Z..X..X
Sample Output
1
1
-1 题意:一张n*m的地图,有一个男孩、一个女孩和两只鬼,每秒男孩走三步,女孩每秒一步,鬼每秒扩张2,也就是k秒后,所有与鬼的曼哈顿距离不大于2*k都会被鬼覆盖,求相遇最短时间。
思路:既然求最短时间,那么很容易想到bfs,对男孩bfs的同时对女孩bfs,当两者相遇的时候就是最短时间。
注:相当于鬼先走,然后人在走;另外注意模块化,我一开始写直接把男孩的三步用三重for去写,其实可以把男孩当成3次一步就好
#include<bits/stdc++.h>
using namespace std; char maps[][];
int ways[][] = {,,,,-,,,-};
int vis[][];
int n,m,t;
int ans;
struct Node
{
int x,y;
Node(int x=,int y=):x(x),y(y) {}
} ghost[],girl,boy; void init()
{
scanf("%d%d",&n,&m);
int cnt = ;
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
{
scanf("%s",maps[i]+);
for(int j=; j<=m; j++)
{
if(maps[i][j] == 'M')
boy = Node(i,j);
else if(maps[i][j] == 'G')
girl = Node(i,j);
else if(maps[i][j] == 'Z')
ghost[cnt++] = Node(i,j);
}
}
} bool check(int x,int y,int k)
{
if(x < || x > n || y < || y > m)
return ;
if(maps[x][y] == 'X')
return ;
if(abs(x - ghost[].x) + abs(y - ghost[].y) <= *k)
return ;
if(abs(x - ghost[].x) + abs(y - ghost[].y) <= *k)
return ;
return ;
}
queue<Node>que[];
bool bfs(int s)
{
int t = que[s].size();
while(t--)
{
Node tmp = que[s].front();
que[s].pop();
if(!check(tmp.x,tmp.y,ans))
continue;
for(int i=; i<; i++)
{
int xx = tmp.x + ways[i][];
int yy = tmp.y + ways[i][];
if(check(xx,yy,ans))
{
if(vis[xx][yy] && (vis[xx][yy]&) != ((s+)&))
return ;
else if(!vis[xx][yy])que[s].push(Node(xx,yy)),vis[xx][yy] = s+;
}
}
}
return ;
} int cal()
{
memset(vis,,sizeof(vis));
while(!que[].empty())que[].pop();
while(!que[].empty())que[].pop();
que[].push(boy);
que[].push(girl);
vis[boy.x][boy.y] = ;
vis[girl.x][girl.y] = ;
ans = ;
while(!que[].empty() || !que[].empty())
{
ans++;
if(bfs())
return ans;
if(bfs())
return ans;
if(bfs())
return ans;
if(bfs())
return ans;
}
return -;
} int main()
{
scanf("%d",&t);
while(t--)
{
init();
printf("%d\n",cal());
}
}
Nightmare Ⅱ HDU - 3085 (双向bfs)的更多相关文章
- HDU - 3085 双向BFS + 技巧处理 [kuangbin带你飞]专题二
题意:有两只鬼,一个男孩女孩被困在迷宫中,男孩每秒可以走三步,女孩只能1步,鬼可以两步且可以通过墙.问男孩女孩是否可以在鬼抓住他们之前会合? 注意:每秒开始鬼先移动,然后两人开始移动. 思路:以男孩和 ...
- hdu 3085(双向bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 思路:双向广搜,每次从M出发,搜三步,从G出发,搜一步,然后就是判断是否走到对方已经走过的格子, ...
- Eight HDU - 1043 (双向BFS)
记得上人工智能课的时候老师讲过一个A*算法,计算估价函数(f[n]=h[n]+g[n])什么的,感觉不是很好理解,百度上好多都是用逆向BFS写的,我理解的逆向BFS应该是从终点状态出发,然后把每一种状 ...
- kuangbin专题 专题二 搜索进阶 Nightmare Ⅱ HDU - 3085
题目链接:https://vjudge.net/problem/HDU-3085 题意:有两个鬼和两个人和墙,鬼先走,人再走,鬼每走过的地方都会复制一个新鬼, 但新鬼只能等待旧鬼走完一次行程之后,下一 ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- HDU 3085 Nightmare Ⅱ(双向BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 题目大意:给你一张n*m地图上,上面有有 ‘. ’:路 ‘X':墙 ’Z':鬼,每秒移动2步,可 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3085 Nightmare Ⅱ 双向BFS
题意:很好理解,然后注意几点,男的可以一秒走三步,也就是三步以内的都可以,鬼可以穿墙,但是人不可以,鬼是一次走两步 分析:我刚开始男女,鬼BFS三遍,然后最后处理答案,严重超时,然后上网看题解,发现是 ...
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
随机推荐
- 如何获取STM32 MCU的唯一ID
前段时间由于应用需要对产品授权进行限制,所以研究了一下有关STM32 MCU的唯一ID的资料,并最终利用它实现了我们的目标. 1.基本描述 在STM32的全系列MCU中均有一个96位的唯一设备标识符. ...
- Confluence 6 配置手动备份
如果你希望关闭自动备份,你可以选择手动导出保存站点.请参考 Manually Backing Up the Site 页面中的内容获得更多的信息. 这些文件没有自动备份在同样的路径中,这些文件存储在 ...
- 学习Spring Boot:(一)入门
微服务 现在微服务越来越火了,Spring Boot热度蹭蹭直升,自学下. 微服务其实是服务化思路的一种最佳实践方向,遵循SOA(面向服务的架构)的思路,各个企业在服务化治理上面的道路已经走得很远了, ...
- Django项目的创建及基本使用
安装步骤 Django是Python进行Web开发的框架,目前应用比较广泛.使用python进行Web开发,能够很快的搭建所需的项目,可以运用于原型开发,也可以部署到实际的应用环境. 使用Django ...
- mysql 安装问题一:由于找不到MSVCR120.dll,无法继续执行代码.重新安装程序可能会解决此问题。
这种错误是由于未安装 vcredist 引起的 下载 vcredist 地址:https://www.microsoft.com/zh-CN/download/details.aspx?id= ...
- Django框架之第三篇模板语法(重要!!!)
一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 一.模板语法之变量:语法为 {{ }}: 在 Django 模板中遍历复杂数据结构的关键 ...
- kali linux 信息收集(Kismet)
1.kismet工具,是一个无线扫描工具,该工具通过测量周围的无线信号,可以扫描到周围附近所用可用的Ap,以及信道等信息.同时还可以捕获网络中的数据包到一个文件中.这样可以方便分析数据包.下面我将详细 ...
- lightoj 1282 取对数的操作
/* 前三位 len=log10n^k(乘积的长度) len=klog10n n^k=x*10^(len-1) x=n^k/10^(len-1) log10x = k*log10n - (len-1) ...
- 2018.8.1 状压 CF482C 题解
noip2016考了一道状压dp,一道期望dp 然而这题是状压期望dp... 所以难度是什么,省选noi吗... 怎么办... 题目大意: 给定n个字符串,甲从中任选出一个串(即选出每个串的概率相同为 ...
- 微信jssdk分享功能开发
先理解下分享: 在app端 ,经常能看见 分享按钮的功能,(分享给朋友,分享到朋友圈,分享到QQ空间等等): https://open.weixin.qq.com/(微信开发平台),这需要到开放平台注 ...