题目描述:

Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the maze neglected to create a fire escape plan. Help Joe escape the maze. Given Joe’s location in the maze and which squares of the maze are on fire, you must determine whether Joe can exit the maze before the fire reaches him, and how fast he can do it. Joe and the fire each move one square per minute, vertically or horizontally (not diagonally). The fire spreads all four directions from each square that is on fire. Joe may exit the maze from any square that borders the edge of the maze. Neither Joe nor the fire may enter a square that is occupied by a wall.

Input

The first line of input contains a single integer, the number of test cases to follow. The first line of each test case contains the two integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The following R lines of the test case each contain one row of the maze. Each of these lines contains exactly C characters, and each of these characters is one of: • #, a wall • ., a passable square • J, Joe’s initial position in the maze, which is a passable square • F, a square that is on fire There will be exactly one J in each test case.

Output

For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.

Sample Input

2

4 4

####

#JF#

#..#

#..#

3 3

###

#J.

#.F

Sample Output

3

IMPOSSIBLE

题意:Joe要逃离一个迷宫,迷宫中有地方起火了,在火开始燃烧的时候Joe也开始逃,火的蔓延方式与Joe的行动方式一样,都是1个单位时间可以往上下左右四个方向各走一格。另外,迷宫内有墙,Joe与火都无法穿墙。现在给你一个图,请问Joe能否从迷宫的边界处逃出而不被火烧到,如果能的话请输出最短的逃脱时间,不能的话输出“IMPOSSIBLE”。其中,‘F’代表火,‘J’代表Joe,‘#’代表墙。

解题思路:参考博客 https://blog.csdn.net/JZQT_T/article/details/38641127

这题首先注意的就是,起火点可能不止一个,也可能没有起火点。其次是如果一个起火点被四周的墙给封闭起来了,那么这个火就相当于没有用了。为了判断Joe走到某个点时这个点是否已经起火,我们需要知道每个点起火的时间。其实可以把每个初始起火点当成一个起火点的邻节点加入队列,这样就只进行了一次BFS,从而获得了每个点起火的时间(如果有些点不会起火,那么这些点的时间就是初始化的INF)。最后从Joe的起点开始BFS一次,如果走到下一个点时那个点已经或者刚好着火了那就不能走,墙也不能走,只要走到边界就说明可以走出迷宫了。

代码:

 //直接预处理出火烧到每一个格子的时间,bfsbfs的时候,已经有火在烧的格子和墙一样处理就行了,火烧时间的预处理也用bfsbfs就行
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; #define N 1001
#define INF 999999999 struct point //定义点结构体
{
int x, y; //坐标
int step; //步数,相当于时间
}; int dx[] = {, -, , }; //方向向量
int dy[] = {, , -, }; //方向向量 int n, m, t;
char map[N][N];
int vis[N][N]; //记录火或者人到达点花费的最少时间
point start, fire; //起点和起火处
queue <point> q; void FireBfs()
{
point a, b;
while (!q.empty())
{
a = q.front();
q.pop();
for (int j=; j<; ++j)
{
int nx = a.x + dx[j];
int ny = a.y + dy[j];
if (nx < || nx >= n || ny < || ny >= m) continue; //越界
if (map[nx][ny] == '#' || vis[nx][ny] <= a.step + ) continue; //墙或者已经走过的点
b.x = nx;
b.y = ny;
//走到这一格的时间=火烧到这一格的时间=上一格的步数加1
b.step = vis[nx][ny] = a.step + ;
q.push(b);
}
}
return;
} void DataProcess()
{
point a, b;
FireBfs();
q.push(start); //将人的起点加入队列准备Bfs
while (!q.empty())
{
a = q.front();
q.pop();
for (int i=; i<; ++i)
{
int nx = a.x + dx[i];
int ny = a.y + dy[i];
if (nx < || nx >= n || ny < || ny >= m) //成功走到边界
{
printf("%d\n", a.step + );
return;
}
if (map[nx][ny] == '#' || vis[nx][ny] <= a.step + ) continue; //遇到墙或者该点起火或者走过
b.x = nx;
b.y = ny;
b.step = vis[nx][ny] = a.step + ;//走到这一步的时间=火烧到这一格的时间=上一格的步数加1
q.push(b);
}
}
puts("IMPOSSIBLE");
return;
} int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d %d", &n, &m);
for (int i=; i<n; ++i)
{
scanf("%s", map[i]);
}
while (!q.empty()) q.pop(); //清空队列
for (int i=; i<n; ++i)
{
for (int j=; j<m; ++j)
{
vis[i][j] = INF; //初始vis
if (map[i][j] == 'J')
{
start.x = i;
start.y = j;
start.step = ;
vis[i][j] = ;
}
else if (map[i][j] == 'F')
{
fire.x = i;
fire.y = j;
fire.step = ;
q.push(fire); //加入队列准备进行火的Bfs
vis[i][j] = ;
}
}
}
DataProcess();
}
return ;
}

Fire! -两次dfs的更多相关文章

  1. HDU 4607 Park Visit 两次DFS求树直径

    两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R:   ans = ...

  2. 蒟蒻浅谈树链剖分之一——两个dfs操作

    树链剖分,顾名思义就是将树形的结构剖分成链,我们以此便于在链上操作 首先我们需要明白在树链剖分中的一些概念 重儿子:某节点所有儿子中子树最多的儿子 重链:有重儿子构成的链 dfs序:按重儿子优先遍历时 ...

  3. 蓝桥杯 大臣的旅费_树的最长度_两次DFS

    #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> ...

  4. 【2-SAT(两次DFS版)】BZOJ1823-[JSOI2010]满汉全席

    [题目大意] 有n个材料,m个评委.每种材料可以被用来做满族菜或汉族菜,m个评委有两种可以让他满意的猜中.问是否可以满足所有评委要求? [思路] 每天只能做三道题,我已经是一个废人了……(葛优躺.jp ...

  5. 两次DFS,POJ(1481)

    题目链接:http://poj.org/problem?id=1481 两次DFS,这里的思路是,没找到*,就说明,有一个骰子,因此,每搜索到一个*,深搜4个方向,并且变为'.',要是搜到'X',就是 ...

  6. 黑科技——树剖两次dfs转一次dfs!

    黑科技--树剖两次\(dfs\)转一次\(dfs\)! 重所周知,树链剖分通常是要\(dfs​\)两次的,就像这样: int Fa[N],dep[N],Sz[N],son[N]; void dfs1( ...

  7. ZOJ 1002:Fire Net(DFS+回溯)

    Fire Net Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose that we have a square city with s ...

  8. HDU1045 Fire Net(DFS枚举||二分图匹配) 2016-07-24 13:23 99人阅读 评论(0) 收藏

    Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...

  9. HDU 1045 Fire Net(DFS 与8皇后问题类似)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. Oracle PGA作用&work_mode

    专有模式下ORACLE会给每个连接分配一个服务进程(Server Process),这个服务进程将为这个连接服务.为这个服务进程分配的内存叫做PGA.PGA不需要Latch也不需要Lock,永远不会发 ...

  2. Oracle管理文件OMF (oracle managed files)

    简化dba的管理操作 1:启用 omf 23:16:04 SYS@orcl> show parameter DB_CREATE_FILE_DEST NAME TYPE VALUE ------- ...

  3. 在Ubuntu 15下搭建V/P/N服务器pptpd安装和配置

    在Ubuntu 15下搭建VPN服务器pptpd安装和配置 在ubuntu下配置vpn的方式有很多种,其中比较常见的是pptpd,它配置简单,但是安全性不高,不过对于一般使用来说足够了,我按照程搭建了 ...

  4. MongoDB的简单操作

    一.简介 二.MongoDB基础知识 三.安装 四.基本数据类型 五.增删改查操作 六.可视化工具 七.pymongo 一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoD ...

  5. k倍区间

    看大佬的代码看了半天,终于算是懂了 标题: k倍区间 给定一个长度为N的数列,A1, A2, … AN,如果其中一段连续的子序列Ai, Ai+1, … Aj(i <= j)之和是K的倍数,我们就 ...

  6. LeetCode(74):搜索二维矩阵

    Medium! 题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例  ...

  7. 如何编辑PDF文件,怎么使用PDF裁剪页面工具

    在编辑PDF文件的时候,往往会有很多的小技巧可以使用,在编辑PDF文件的时候,怎么对文件的页面进行裁剪呢,不会的话,看看下面的文章吧,小编已经为大家整理好了哦. 1.打开运行PDF编辑器,在编辑器中打 ...

  8. python WebDriver如何处理右键菜单

    WebDriver如何处理右键菜单 一.背景 在学习selenium webdriver的过程中,遇到这样一个问题.ActionChains类中提供了context_click的方法,它可以用来在we ...

  9. Python函数之递归函数

    递归函数的定义:在这个函数里再调用这个函数本身 最大递归深度默认是997或者998,python从内存角度做的限制 优点:代码变简单 缺点:占内存 一:推导年龄 问a的值是多少: a 比 b 小2,b ...

  10. IEDA序列化设置