题目链接

Problem Description

Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were trapped in a big maze separately. More terribly, there are two ghosts in the maze. They will kill the people. Now little erriyue wants to know if he could find his girl friend before the ghosts find them.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.

Input

The 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

Output

Output 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

 11-1

AC

  • 对鬼魂的处理:每次先计算鬼经过当前时间能否到达M和G的位置(鬼魂先移动),然后判断当M和G移动之后能否到达(移动之后位置的合法性)
  • 对于M和G:记录他们所走的位置,因为虽然可能不是同时到达,但是如果他们都曾经到达一个地方(首先这个地方已经安全),那么其中一个人完全可以等着另一个人
  • 用两个BFS求最短距离
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[802][802];
bool vis[2][802][802];
vector<pair<int, int> > z;
queue<pair<int, int> > q[2];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};bool judge(int x, int y, int time) {if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] == 'X')return false;if (fabs(x - z[0].first) + fabs(y - z[0].second) <= time * 2)return false;if (fabs(x - z[1].first) + fabs(y - z[1].second) <= time * 2)return false;return true;
}
bool bfs(int t, int time) {int len = q[t].size();// 当前所有状态移动一位 while (len--) {int x = q[t].front().first;int y = q[t].front().second;q[t].pop();// 鬼魂先走 if (!judge(x, y, time)) continue;int xx, yy;for (int i = 0; i < 4; ++i) {xx = x + dx[i];yy = y + dy[i];if (!judge(xx, yy, time) || vis[t][xx][yy]) continue;if (vis[(t ^ 1)][xx][yy])   return true;if (vis[t][xx][yy]) continue;vis[t][xx][yy] = true;q[t].push(make_pair(xx, yy));}}return false;
}int main() {
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endifint t;scanf("%d", &t);while (t--) {memset(vis, false, sizeof(vis));scanf("%d%d", &n, &m);for (int i = 0; i < n; ++i) {scanf("%s", a[i]);for (int j = 0; j < m; ++j) {if (a[i][j] == 'M')q[0].push(make_pair(i, j)), vis[0][i][j] = true;if (a[i][j] == 'G')q[1].push(make_pair(i, j)), vis[1][i][j] = true;if (a[i][j] == 'Z')z.push_back(make_pair(i, j));}}int ans = 1;while (1) {// M走三步 if (bfs(0, ans))    break;if (bfs(0, ans))    break;if (bfs(0, ans))    break;// G走一步 if (bfs(1, ans))    break;if (q[0].empty() && q[1].empty()) {ans = -1;break;} ++ans;}printf("%d\n", ans);while (!q[1].empty()) {q[1].pop();}while (!q[0].empty()) {q[0].pop();}z.clear();}return 0;
} 

HDU Problem - 3085 Nightmare Ⅱ(双向BFS)相关推荐

  1. HDU - 3085 Nightmare Ⅱ(双向bfs)

    题目链接:点击查看 题目大意:给出一个迷宫,一个男孩和一个女孩还有两只鬼,男孩每秒钟走3格,女孩每秒钟走1格,鬼每秒钟向四周分裂2格,问男孩和女孩能否在鬼占领迷宫之前汇合,能的话输出汇合时间,否则输出 ...

  2. HDU3085 Nightmare Ⅱ —— 双向BFS + 曼哈顿距离

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3085 Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Other ...

  3. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

  4. HDU 3085 Nightmare Ⅱ【BFS +曼哈顿距离+综合性较强】

    Nightmare Ⅱ Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total S ...

  5. G - Nightmare Ⅱ (双向BFS)

    题目链接:SDTBU-ACM集训队暑期集训---第一讲 - Virtual Judge Last night, little erriyue had a horrible nightmare. He ...

  6. 专题训练二 搜索进阶 HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离)

    HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离) Problem Description Last night, little erriyue had a horrible ...

  7. hdu 3567(八码数 + 双向bfs)

    题目连接:https://vjudge.net/contest/353606#problem/B 参考文章:https://blog.csdn.net/laaahu/article/details/9 ...

  8. 浅谈迷宫搜索类的双向bfs问题(例题解析)

    文章目录 前言 bfs类问题 双向bfs 例题实战 前言 文章若有疏忽还请指正,更多精彩还请关注公众号:bigsai 在搜索问题中,以迷宫问题最具有代表性,无论是八皇后的回溯问题,还是dfs找出口,b ...

  9. 数据结构实验之图论四:迷宫探索_迷宫搜索类的双向bfs问题(例题详解)

    前言 文章若有疏忽还请指正! 更多精彩还请关注公众号:bigsai 头条号:一直码农一直爽 在搜索问题中,以迷宫问题最具有代表性,无论是八皇后的回溯问题,还是dfs找出口,bfs找最短次数等等题目的问 ...

最新文章

  1. ant-design-pro Login 中的 UserName 和 Password 的验证规则 rules
  2. 【消息中间件】Spring整合RabbitMQ
  3. iAPP(05)自习室占座
  4. 转移指令的原理---汇编学习笔记
  5. DevOps时代,企业数字化转型需要强大的工具链
  6. 计算机网络(一)——一些概念
  7. 【操作系统】进程管理(二)
  8. MSP430学习笔记10-ADC采集1602显示
  9. 汽车工程大专业细分学科类别小探
  10. 深度优先遍历和广度优先遍历
  11. Unity功能实现——解析OBJ模型文件
  12. CR渲染太慢,怎么优化CR渲染任务
  13. 2018十大网络用语新鲜出炉,skr入围榜三!
  14. LookupError: Resource omw-1.4 not found. nltk3.7查找近反义词解决方法
  15. 新浪 透视java_(JAVA)MyColorCube7(透视效果)
  16. iOS 上的插件化设计
  17. 高精地图众包生产模式
  18. Android框架排行榜,上百项资源汇总不容错过
  19. linux 文件格式elf,linux ELF 文件格式 | ZION
  20. net use 访问远程电脑

热门文章

  1. YTKNetwork源码详解
  2. 网站如何从http升级成https
  3. Xtrabackup 安装使用
  4. 查看wcf服务中方法测试客户端
  5. 【初级】String str= ac,42,123,sd Fa,c df,4,acdf,5ewRRre ;1.把字符串按,进行分割
  6. 用for和do..while两种方法:键盘录入一个数 求阶乘的和
  7. XSSFORK:新一代XSS自动扫描测试工具(精)
  8. Masm for Windows集成开发环境编写汇编程序
  9. 【数据结构与算法】之深入解析“把二叉搜索树转换为累加树”和“从二叉搜索树到更大和树”的求解思路与算法示例
  10. 【数据结构与算法】之深入解析“二叉树的前序遍历”的求解思路与算法示例