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. mybatis教程2(配置文件)

    MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置(settings)和属性(properties)信息.文档的顶层结构如下: configuration 配置 propertie ...

  2. Python循环文件推荐的方式,可用于读取文本最后一行或删除指定行等

    读取文本最后一行: f = open('test11.txt', 'rb') for i in f: offset = -16 while True: f.seek(offset, 2) data = ...

  3. Django组件之cookie与session

    一.引子 http协议是无状态的,就是它不会记录请求和响应的任何信息,比如你访问一个服务器的一个网页时,先要你登录一下,然后进入网页,但当你要进入这个服务器的另一个网页时,它照常不会知道刚才你已经登录 ...

  4. 【RabbitMQ】8、RabbitMQ之mandatory和immediate

    1. 概述 mandatory和immediate是AMQP协议中basic.publish方法中的两个标识位,它们都有当消息传递过程中不可达目的地时将消息返回给生产者的功能.对于刚开始接触Rabbi ...

  5. GDB使用技巧

    最近使用GDB比较多,发现除了最常用的run.break.continue.next等命令的基本用法外,还有一些非常有用的命令和用法,能让你更加得心应手地使用GDB,在这里做了一下简单的总结. 1. ...

  6. NULL 和 0

    Question: What is the difference from NULL and "0"? Example: return NULL; return 0; Answer ...

  7. FHQ Treap小结(神级数据结构!)

    首先说一下, 这个东西可以搞一切bst,treap,splay所能搞的东西 pre 今天心血来潮, 想搞一搞平衡树, 先百度了一下平衡树,发现正宗的平衡树写法应该是在二叉查找树的基础上加什么左左左右右 ...

  8. 组件化和 React

    一,对组件化的理解 1,组件的封装 -视图 -数据 -变化逻辑(数据驱动视图变化) 例: import React, { Component } from 'react'; import List f ...

  9. Java并发编程(四)synchronized

    一.synchronized同步方法或者同步块 在了解synchronized关键字的使用方法之前,我们先来看一个概念:互斥锁,顾名思义:能到达到互斥访问目的的锁. 举个简单的例子:如果对临界资源加上 ...

  10. 基于bootstrap的双日历插件 daterangepicker

    我遇到需求是要求我将daterangepicker的一个双日期选择格式修改成两个单日期格式的日期选择框(方便手机端显示),要求如下: 1.两个单日期格式分别为开始日期和结束日期 2.开始日期可选择范围 ...