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 ...
随机推荐
- kindeditor用法简单介绍(转)
1,首先去官网下载http://www.kindsoft.net/ 2,解压之后如图所示: 由于本人做的是用的是JSP,所以ASP,PHP什么的就用不上了,直接把那些去掉然后将整个文件夹扔进Myecl ...
- js——正则表达式
1. 创建一个正则表达式 var patt=new RegExp(pattern,modifiers);var patt=/pattern/modifiers; var index = str.sea ...
- iOS ReplayKit 录屏 框架的使用
在需要使用录屏的 地方 引入 头文件 #import <ReplayKit/ReplayKit.h> 添加代理 RPPreviewViewControllerDelegate 因为 iOS ...
- Confluence 6 修改特定的空间标识图片
空间管理员可以为他们管理的空间修改空间标识图片.这个修改将会覆盖默认的空间标识图片,任何对默认空间图标表示的修改将不会对已经修改的空间标识图片产生影响.请查看上面的例子中的 'Sample Space ...
- selenium关于断言的使用
基本介绍: Selenium工具专门为WEB应用程序编写的一个验收测试工具. Selenium的核心:browser bot,是用JAVASCRIPT编写的. Selenium工具有4种:Seleni ...
- python网络爬虫笔记(八)
一.pthon 序列化json格式 1.将python内置对象转换成json 模块,dumps()方法返回的是一个str,内容是标准的JSON,dump()方法可以直接吧JSON写入一个file-li ...
- 阿里云人脸识别测试接口出错 返回Body:{ "errno": 3002, "err_msg": "ILLEGAL_PARAMETERS", "request_id": "672cba83-cf93-4ef4-9ce5-d87e51601632" }
错误信息如下 返回Body:{ "errno": 3002, "err_msg": "ILLEGAL_PARAMETERS", ...... ...
- java 多线程中的锁的类别及使用
目前在Java中存在两种锁机制: synchronized Lock Lock接口及其实现类是JDK5增加的内容,其作者是大名鼎鼎的并发专家Doug Lea. 数据同步需要依赖锁,那锁的同步又依赖谁? ...
- 集腋成裘-05-angularJS -初识angular
私以为angular的最大特点是:只关注数据 1.1 angular之:双向绑定 <!DOCTYPE html> <html ng-app=""> < ...
- Cannot uninstall 'html5lib'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
如标题,安装Tensorflow-gpu时遇到的完整问题 Cannot uninstall 'html5lib'. It is a distutils installed project and th ...