Fire! -两次dfs
题目描述:
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的更多相关文章
- HDU 4607 Park Visit 两次DFS求树直径
两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R: ans = ...
- 蒟蒻浅谈树链剖分之一——两个dfs操作
树链剖分,顾名思义就是将树形的结构剖分成链,我们以此便于在链上操作 首先我们需要明白在树链剖分中的一些概念 重儿子:某节点所有儿子中子树最多的儿子 重链:有重儿子构成的链 dfs序:按重儿子优先遍历时 ...
- 蓝桥杯 大臣的旅费_树的最长度_两次DFS
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> ...
- 【2-SAT(两次DFS版)】BZOJ1823-[JSOI2010]满汉全席
[题目大意] 有n个材料,m个评委.每种材料可以被用来做满族菜或汉族菜,m个评委有两种可以让他满意的猜中.问是否可以满足所有评委要求? [思路] 每天只能做三道题,我已经是一个废人了……(葛优躺.jp ...
- 两次DFS,POJ(1481)
题目链接:http://poj.org/problem?id=1481 两次DFS,这里的思路是,没找到*,就说明,有一个骰子,因此,每搜索到一个*,深搜4个方向,并且变为'.',要是搜到'X',就是 ...
- 黑科技——树剖两次dfs转一次dfs!
黑科技--树剖两次\(dfs\)转一次\(dfs\)! 重所周知,树链剖分通常是要\(dfs\)两次的,就像这样: int Fa[N],dep[N],Sz[N],son[N]; void dfs1( ...
- ZOJ 1002:Fire Net(DFS+回溯)
Fire Net Time Limit: 2 Seconds Memory Limit: 65536 KB Suppose that we have a square city with s ...
- 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 ...
- HDU 1045 Fire Net(DFS 与8皇后问题类似)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- Idea 调试快捷键
F9 resume programe 恢复程序 Alt+F10 show execution point 显示执行断点 F8 Step Over ...
- 'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件
今天安装完MYSQL8.0的版本,根据课本的提示,在CMD里运行,出现了'mysql' 不是内部或外部命令,也不是可运行的程序或批处理文件.在网上搜了一下,他的解决方法是这样的: 1.设置一下环境变量 ...
- Confluence 6 手动备份
Confluence 的 Attachment Storage Configuration 可以配置 Confluence 将附件存储在 home directory,或者是存储在数据库中. Dat ...
- 二.Rsync备份服务
自己动手部署一遍 期中架构-第二章-备份服务笔记====================================================================== 01. 课 ...
- Zookeeper安装(本地,伪分布式,集群)
概述 ZooKeeper是一个分布式开源框架,提供了协调分布式应用的基本服务,它向外部应用暴露一组通用服务——分布式同步(Distributed Synchronization).命名服务(Namin ...
- bzoj 4816
这题是莫比乌斯反演的典型题也是很有趣的题. 题意:求,其中f为为斐波那契数列 那么首先观察一下指数,发现是我们熟悉的形式,可以转化成这样的形式: 令T=kd,且假设n<m,有: 令 则原式= 这 ...
- git bash here 的 ~/.bashrc 配置文件。和 vue/cli 3. 0 的 .vuerc文件(preset )
今天就来讲一下git有关的小技巧,.bashrc文件是用户配置文环境变量的文件,每次git bash会首先运行里面的内容 1.自动运行 每次进入git bash都会先读取.bashrc里面的内容,因此 ...
- hdu 5183(Hash处理区间问题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5183 题意:给出一个n个元素的数组,现在要求判断 a1-a2+a3-a4+.....+/-an 中是否 ...
- pandas之系列操作(一)
1.读Excel: # coding=utf-8 import pandas as pd import pymysql sql_select =" xxxxx " con = py ...
- PowerDesigner逆向生成MYSQL数据库表结构总结
由于日常数据建模经常使用PowerDesigner,使用逆向工程能更加快速的生成模型提高效率,所以总结使用如下: 1. 安装MYSQL的ODBC驱动 Connector/ODBC 5.1.1 ...