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 ...
随机推荐
- winform的水印TextBox
public partial class WaterTextBox : TextBox { private readonly Label lblwaterText = new Label(); pub ...
- 判断iOS版本号
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) { }
- easyui datagrid 隔行变色
easyui datagrid 隔行变色 一:实现样图 二:实现代码 $('#dataGrid').datagrid({ rowStyler:function(index,row){ if (row ...
- 关于python3链接虚拟机MongoDB 遇到的问题总结
pymongo.errors.ServerSelectionTimeoutError: 192.168.12.230:27017: [Errno 61] Connection refused 1.如果 ...
- (七)STL适配器
1.适配器是稍微修改某些功能,比如三个参数改为两个参数,函数的名称改一下等等,可以出现在容器.迭代器和仿函数中. 2.适配器相当于对某个东西进行封装,例如A是B的适配器,则真正的功能实现是在B中,可以 ...
- vuex action 与mutations 的区别
面试没说清楚.这个太丢人回来整理下: 事实上在 vuex 里面 actions 只是一个架构性的概念,并不是必须的,说到底只是一个函数,你在里面想干嘛都可以,只要最后触发 mutation 就行.异步 ...
- javaScript遍历对象、数组总结
javaScript遍历对象总结 1.使用Object.keys()遍历 返回一个数组,包括对象自身的(不含继承的)所有可枚举属性(不含Symbol属性). var obj = {'0':'a ...
- LeetCode(106):从中序与后序遍历序列构造二叉树
Medium! 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 中序遍历 inorder = [9,3,15,20,7] 后序遍历 posto ...
- js获取到的页面中的checkbox选中的项
需求描述:列表第一列是checkbox name和value都是id 想通过复选框的勾选状态来获取id,在js中获取 js代码: var checkId=$("input[name='che ...
- Spring-data-redis: serializer实例
spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是非常便捷.sdr提供了4种内置的serializer: JdkSerializationRe ...