kuangbin fire搜索bfs
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
要分成两条路,一条是火烧得,另一条是人走的,把火烧到每一个地方的时间记录下来
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
int x,y;
int step;
node(int x=,int y=) : x(x),y(y) {} //构造函数
};
const int inf=0x3f3f3f;
int to[][]={,,,,,-,-,},t[][],n,m,sx,sy,ou;
bool vit[][];
char a[][];
void bfs1()
{
queue<node>que;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(a[i][j]=='F')
{
t[i][j]=;
que.push(node(i,j));
}
}
}
while(!que.empty())
{
node exa=que.front();
que.pop();
for(int i=;i<;i++)
{
int tx=exa.x+to[i][];
int ty=exa.y+to[i][]; if(a[tx][ty]=='#') continue;
if(tx<||tx>n||ty<||ty>m) continue;
if(t[exa.x][exa.y]+>=t[tx][ty]) continue;//不用vis,因为时间短的优先考虑,重点!!! t[tx][ty]=t[exa.x][exa.y]+;//记录时间
que.push(node(tx,ty));
}
}
} void bfs2()
{
node ee;
queue<node>que;
ee.x=sx;
ee.y=sy;
ee.step=;
que.push(ee);
vit[ee.x][ee.y]=;
while(!que.empty())
{
ee=que.front();
node zz;
que.pop();
if(ee.x==||ee.y==||ee.x==n||ee.y==m)
{
ou=ee.step+;
return ;
}
for(int i=;i<;i++)
{ zz.x=ee.x+to[i][];
zz.y=ee.y+to[i][];
zz.step=ee.step+; //step记录每一个位置所用的时间与火的时间作比较 if(zz.x<||zz.y<||zz.x>n||zz.y>m) continue;
if(a[zz.x][zz.y]=='#') continue;
if(vit[zz.x][zz.y]) continue;
if(zz.step>=t[zz.x][zz.y]) continue; vit[zz.x][zz.y]=;
que.push(zz);
}
}
} int main()
{
int c;
cin>>c;
while(c--)
{
memset(vit,,sizeof(vit));
memset(t,inf,sizeof(t));
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%s",a[i]+);
for(int j=;j<=m;j++)
{
if(a[i][j]=='J'){sx=i;sy=j;}
}
}
ou=inf;
bfs1();
bfs2();
if(ou>=inf) printf("IMPOSSIBLE\n");
else printf("%d\n",ou);
}
return ;
}
kuangbin fire搜索bfs的更多相关文章
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- 【算法入门】广度/宽度优先搜索(BFS)
广度/宽度优先搜索(BFS) [算法入门] 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较 ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)
深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...
- 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)
需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...
- 广度优先搜索 BFS算法
广度优先搜索算法(Breadth-First-Search,BFS),又称作宽度优先搜索.BFS算法是从根节点开始,沿着树的宽度遍历树的节点.如果所有节点均被访问,则算法中止. 算法思想 1.首先将根 ...
- 深度优先搜索DFS和广度优先搜索BFS简单解析
转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- 【数据结构与算法Python版学习笔记】图——词梯问题 广度优先搜索 BFS
词梯Word Ladder问题 要求是相邻两个单词之间差异只能是1个字母,如FOOL变SAGE: FOOL >> POOL >> POLL >> POLE > ...
- 广度优先搜索 BFS 学习笔记
广度优先搜索 BFS 学习笔记 引入 广搜是图论中的基础算法之一,属于一种盲目搜寻方法. 广搜需要使用队列来实现,分以下几步: 将起点插入队尾: 取队首 \(u\),如果 $u\to v $ 有一条路 ...
随机推荐
- js节流函数和js防止重复提交的N种方法
应用情景 经典使用情景:js的一些事件,比如:onresize.scroll.mousemove.mousehover等: 还比如:手抖.手误.服务器没有响应之前的重复点击: 这些都是没有意义的,重复 ...
- Spark2.1.0——内置Web框架详解
Spark2.1.0——内置Web框架详解 任何系统都需要提供监控功能,否则在运行期间发生一些异常时,我们将会束手无策.也许有人说,可以增加日志来解决这个问题.日志只能解决你的程序逻辑在运行期的监控, ...
- SPI Flash(W25Q16DV) 驱动
大体上可分为以下几个部分: 1.注册设备驱动 spi_register_driver 2.分配 mtd_info 结构体 3.配置 mtd_info 结构体 4.注册 mtd_info 结构体 构建 ...
- 你不知道的Linux(持续更新中)
1.关于GNU.Linux.GNU/Linux三者的关系 GNU 项目创始于一九八四年,旨在开发一个类似 Unix ,且为自由软件的完整的操作系统: GNU 系统.(也可把GNU看成一个自由软件工程) ...
- T-SQL:批GO使用实例(十四)
批是由客户端应用程序作为一个单元发送给SQL Server 执行的一条或多条语句 如果批中出现错误就整个批都不会交给SQL SERVER 执行 PRINT '第一批';GO -- Invalid b ...
- c# 读取xml 某个节点值
一.xml格式如下: <?xml version="1.0" encoding="UTF-8"?><Freight> <freig ...
- idea创建maven项目速度慢?别急,这有三种方案
困扰 Intellij idea是一款非常强大的编辑器,可以很方便地帮我们创建maven项目,有用过的同学应该都深有体会,但我们经常会遇到一个困扰,那就是用idea创建maven项目时,速度很慢,往往 ...
- java_分解质因数
题目内容: 每个非素数(合数)都可以写成几个素数(也可称为质数)相乘的形式,这几个素数就都叫做这个合数的质因数.比如,6可以被分解为2x3,而24可以被分解为2x2x2x3. 现在,你的程序要读入一个 ...
- IDEA maven 项目如何上传到私服仓库
前言:idea maven 发布版本到私服(快照和正式版).我有个项目( jar 包源码),其他 maven 项目能直接引入依赖就最好了,所以必须将这个 jar 包源码发布到 maven 私服仓库里去 ...
- HTML的head标签
前端开发工具介绍: Hbuilder:可以快速的生成HTML标准文档结构,集成了很多方便的快捷键.--------------------------------------------------- ...