题目传送门

题意:一个人去救女朋友,两个人都在运动,还有鬼在"扩散",问最少几秒救到女朋友

分析:开两个队列来表示两个人走过的路,一个人走到的地方另一个人已经vis了,那么就是相遇了,鬼就用曼哈顿距离判断.

#include <bits/stdc++.h>
using namespace std;const int N = 8e2 + 5;
char maze[N][N];
int n, m;
bool vis[N][N][2];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
struct Point    {int x, y;Point()   {}Point(int x, int y)   : x (x), y (y) {}
};
queue<Point> que[2];
Point mm, gg, gho[2];int get_dis(int x, int y, int ex, int ey)  {return abs (ex - x) + abs (ey - y);
}bool check2(int x, int y, int tim) {for (int i=0; i<2; ++i)  {if (get_dis (x, y, gho[i].x, gho[i].y) <= 2 * tim) return false;}return true;
}bool check(int x, int y, int typ)  {if (x < 1 || x > n || y < 1 || y > m || maze[x][y] == 'X') return false;else   return true;
}void init(void)    {while (!que[0].empty ())   que[0].pop ();while (!que[1].empty ())  que[1].pop ();memset (vis, false, sizeof (vis));
}bool BFS(int typ, int tim) {int sz = que[typ].size ();while (sz--)    {Point u = que[typ].front ();  que[typ].pop ();if (!check (u.x, u.y, typ) || !check2 (u.x, u.y, tim))  continue;for (int i=0; i<4; ++i)  {int tx = u.x + dx[i], ty = u.y + dy[i];if (!check (tx, ty, typ) || !check2 (tx, ty, tim))  continue;if (vis[tx][ty][typ^1])    return true;if (vis[tx][ty][typ])   continue;vis[tx][ty][typ] = true;que[typ].push (Point (tx, ty));}}return false;
}int run(void)  {init ();   vis[mm.x][mm.y][0] = true;vis[gg.x][gg.y][1] = true;que[0].push (Point (mm.x, mm.y));que[1].push (Point (gg.x, gg.y));int step = 0;while (!que[0].empty () || !que[1].empty ())  {++step;for (int i=0; i<3; ++i) {if (BFS (0, step)) return step;}if (BFS (1, step)) return step;}return -1;
}int main(void) {int T; scanf ("%d", &T);while (T--)  {scanf ("%d%d", &n, &m);for (int i=1; i<=n; ++i)   {scanf ("%s", maze[i] + 1);}int t = 0;for (int i=1; i<=n; ++i)   {for (int j=1; j<=m; ++j)    {if (maze[i][j] == 'M') {mm.x = i; mm.y = j;}else if (maze[i][j] == 'G')  {gg.x = i; gg.y = j;}else if (maze[i][j] == 'Z')  {gho[t].x = i; gho[t++].y = j;}}}printf ("%d\n", run ());}return 0;
}

  

转载于:https://www.cnblogs.com/Running-Time/p/4992973.html

BFS(双向) HDOJ 3085 Nightmare Ⅱ相关推荐

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

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

  2. HDU Problem - 3085 Nightmare Ⅱ(双向BFS)

    题目链接 Problem Description Last night, little erriyue had a horrible nightmare. He dreamed that he and ...

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

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

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

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

  5. LeetCode 127. 单词接龙(图的BFS/双向BFS)

    文章目录 1. 题目 2. 图的BFS解题 2.1 单向BFS 2.2 双向BFS !厉害了 1. 题目 给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord ...

  6. POJ1915 BFS双向BFS

    俩月前写的普通BFS #include <cstdio> #include <iostream> #include <cstring> #include <q ...

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

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

  8. BFS、双向BFS和A*

    BFS.双向BFS和A* Table of Contents 1. BFS 2. 双向BFS 3. A*算法 光说不练是没用的,我们从广为人知的POJ 2243这道题谈起:题目大意:给定一个起点和一个 ...

  9. 程序员面试金典 - 面试题 17.22. 单词转换(BFS)

    1. 题目 给定字典中的两个词,长度相等. 写一个方法,把一个词转换成另一个词, 但是一次只能改变一个字符. 每一步得到的新词都必须能在字典中找到. 编写一个程序,返回一个可能的转换序列.如有多个可能 ...

  10. LeetCode 126. 单词接龙 II(图的BFS)

    1. 题目 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列. 转换需遵循如下规则: 每次转换只能 ...

最新文章

  1. Debian7.0中Fcitx输入法无可用前端问题解决
  2. 很经典的117句惊世良言
  3. 杨英(帮别人名字作诗)
  4. Spring总结之SpringMvc下
  5. HDU 5384 Danganronpa (2015年多校比赛第8场)
  6. 阿里云引领云原生进化,智能、互联、可信三位一体
  7. bzoj3203: [Sdoi2013]保护出题人
  8. 【爬虫剑谱】一卷3章 软件篇-Anaconda的安装及配置
  9. Android之Camera预览
  10. 2.描述性统计的matlab 实现
  11. requirejs加载layerdate.js遇到的各种坑
  12. Revit二次开发——叠合板自动配筋(2)
  13. 合并两个有序数组(Python)
  14. Cuda驱动安装/更新
  15. 电视游戏大厅现状:“渠道为王”遭遇水土不服
  16. 青蛙的约会(POJ 1061 扩展欧几里德算法)
  17. SIM7600CE GSM/UMTS/LTE天线设计指南
  18. LaTeX中如何使图片插入紧跟在当前文字之后
  19. Windows Phone : 三屏一云战略的新里程碑
  20. 2018年广东工业大学文远知行杯新生程序设计竞赛 1006 那天的延长线在今天β...

热门文章

  1. 对象内存布局 (9)
  2. 摘抄 ander图片上传
  3. EonerCMS——做一个仿桌面系统的CMS(十四)
  4. Understanding Bootstrap Of Oracle Database
  5. MATLAB中的imagesc
  6. NoteBurner iTunes DRM Audio Converter for Mac(苹果DRM音频转换器)
  7. 关于Firefox浏览器设置默认字体的教程
  8. 哦?你会视频剪辑?是的。好,录用你!
  9. Flutter入门(1)—环境配置
  10. 1001 害死人不偿命的(3n+1)猜想 (15)