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的更多相关文章

  1. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  2. 【算法入门】广度/宽度优先搜索(BFS)

    广度/宽度优先搜索(BFS) [算法入门] 1.前言 广度优先搜索(也称宽度优先搜索,缩写BFS,以下采用广度来描述)是连通图的一种遍历策略.因为它的思想是从一个顶点V0开始,辐射状地优先遍历其周围较 ...

  3. 深度优先搜索DFS和广度优先搜索BFS简单解析(新手向)

    深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每个点仅被访问一次,这个过程就是图的遍历.图的遍历常用的有深度优先搜索和广度优先搜索,这两者对于有向图和无向图 ...

  4. 利用广度优先搜索(BFS)与深度优先搜索(DFS)实现岛屿个数的问题(java)

    需要说明一点,要成功运行本贴代码,需要重新复制我第一篇随笔<简单的循环队列>代码(版本有更新). 进入今天的主题. 今天这篇文章主要探讨广度优先搜索(BFS)结合队列和深度优先搜索(DFS ...

  5. 广度优先搜索 BFS算法

    广度优先搜索算法(Breadth-First-Search,BFS),又称作宽度优先搜索.BFS算法是从根节点开始,沿着树的宽度遍历树的节点.如果所有节点均被访问,则算法中止. 算法思想 1.首先将根 ...

  6. 深度优先搜索DFS和广度优先搜索BFS简单解析

    转自:https://www.cnblogs.com/FZfangzheng/p/8529132.html 深度优先搜索DFS和广度优先搜索BFS简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...

  7. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  8. 【数据结构与算法Python版学习笔记】图——词梯问题 广度优先搜索 BFS

    词梯Word Ladder问题 要求是相邻两个单词之间差异只能是1个字母,如FOOL变SAGE: FOOL >> POOL >> POLL >> POLE > ...

  9. 广度优先搜索 BFS 学习笔记

    广度优先搜索 BFS 学习笔记 引入 广搜是图论中的基础算法之一,属于一种盲目搜寻方法. 广搜需要使用队列来实现,分以下几步: 将起点插入队尾: 取队首 \(u\),如果 $u\to v $ 有一条路 ...

随机推荐

  1. 使用Asp.Net Core MVC 开发项目实践[第一篇:项目结构说明]

    先从下图看整体项目结构: Mango.Manager: 为后台管理项目 Mango.Web: 为前台项目 Mango.Framework.Core: 为常用的基础操作类项目 Mango.Framewo ...

  2. noip之后的一些感受

    你经历过绝望吗,那种希望完全破碎,眼前看不到光亮,不知道下一步怎么走,不知道接下来应该如何生活的那种绝望? 我经历过. 2018.11.12 下午 秦皇岛到石家庄的高铁上 听着同学兴高采烈的讨论,自己 ...

  3. VSCode git Warning LF will be replaced by CRLF

    本文参考自:http://www.yulongjun.com/linux/20170518-08-lf-cr/ 我们打开Visual Studio Code编辑器,可以看到右下角有这个LF,这是VS ...

  4. 如何获得刚刚插入数据的id

    create table tblInsert ( id ,) primary key, name ) ); insert into tblInsert(name) values('张三'); sele ...

  5. LINQ 【高级查询】

    using (Data0216DataContext con = new Data0216DataContext()) {      List<Users> ulist = con.Use ...

  6. eclipse使用struts2找不到action方法或找不到action的错误记录

    在确认web.xml已经配置, 配置好struts.xml , 代码没有报错, jar包没有问题, 服务器也没有问题, 代码逻辑没有问题, 关键字方法名action都没有写错, 可以运行旧的相同的代码 ...

  7. Spring基于纯注解方式的使用

    经过上篇xml与注解混合方式,对注解有了简单额了解,上篇的配置方式极大地简化了xml中配置,但仍有部分配置在xml中进行,接下来我们就通过注解的方式将xml中的配置用注解的方式实现,并最终去掉xml配 ...

  8. JS之iscroll.js的使用详解

    入门 Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化.每个页面上的iScroll实例数目在设备的CPU和内存能承受的范围内是没有限制的. 尽可能保持DOM结构的简洁.iScroll使用 ...

  9. unity相机跟随Player常用方式

    固定跟随,无效果(意义不大) public class FollowPlayer : MonoBehaviour { public Transform Player; private Vector3 ...

  10. Vue和React的对比

    今晚我们来搞一搞Vue和React的对比好吧,话不多说今天我们直接开搞可好,各位小老板,开始吧 1. react整体是函数式的思想,把组件设计成纯组件,状态和逻辑通过参数传入, 所以在react中,是 ...