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. MRTG在Windows平台的安装及使用

    MRTG (Multi Router Traffic Grapher)是一款监控网络流量负载的免费软件,目前利用MRTG已经开发出了各式各样的统计系统: 1.系统资源负载统计,例如:磁盘空间.CPU负 ...

  2. 细说移动端 经典的REM布局 与 新秀VW布局

    和以往一样,本次项目也放到了 Github 中,欢迎围观 star ~ 1. 前言 2. 基本概念 3. REM布局 4. VW布局 实现单边边框1px 实现多边边框1px 实现边框圆角 实现容器固定 ...

  3. 初识SqlLite ---.net连接数据库

    Sqlite 是一款轻量级的关系型数据库,以小巧和嵌入式闻名.以前只是听说,现在终于忍不住要尝试下.本文的初衷是为.net平台的使用者提供帮助. Sqlite有专门为VS2010开发的程序包,大家可以 ...

  4. Java中的集合迭代器

    集合的迭代器 任何集合都有迭代器. 任何集合类,都必须能以某种方式存取元素,否则这个集合容器就没有任何意义. 迭代器,也是一种模式(也叫迭代器模式).在java中它是一个对象,其目的是遍历并选中其中的 ...

  5. [PHP]算法- 二叉树的深度的PHP实现

    二叉树的深度: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 思路: 1.非递归层序遍历 2.使用辅助队列,根结点先入队列 ...

  6. Flask在Pycharm开启调试模式

       一.Flask在Pycharm2018前的版本只需设置(两种方法之一): 1. 直接设置app的debug为true: app.debug=true 2. 把debug=true作为参数,传入到 ...

  7. canvas-star2.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. JS 数组位置方法 indexOf()和lastIndexOf()的理解

    var numbers = [1,3,5,7,9,4,3,2,1]; console.log(numbers.indexOf(5)); //从数组的0位开始查找 5 位于数组里面的位置 输出2 首先 ...

  9. 【代码笔记】Web-HTML-标题

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  10. 【读书笔记】iOS-设计简单的Frenzic式益智游戏

    如果你决定用UIView动画或Core Animation,一定要编写一些测试用例,模拟游戏可能遇到的要求最高的动画,另外不要忘记播放声音.不要等到最后才增加声音,因为在iPhone上播放音乐和音效确 ...