搜索(BFS)
Problem B: Fire!
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 Specification
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.
Sample Input
2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F
Output Specification
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.
Output for Sample Input
3
IMPOSSIBLE
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <string>
#include <queue>
#include <vector> using namespace std; #define INF 1<<29
#define eps 1e-8
#define A system("pause")
#define rep(i,h,n) for(int i=(h);i<=(n);i++)
#define ms(a,b) memset((a),(b),sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1 const int maxn=2000+5;
int dx[]={0,1,-1,0};
int dy[]={1,0,0,-1};
struct dot
{
int x,y;
bool isfire;
int time;
}; int vis[maxn][maxn];
char map[maxn][maxn];
int test,n,m; inline bool in(dot sgx)
{
if(sgx.x>=0&&sgx.x<n&&sgx.y>=0&&sgx.y<m) return true;
return false;
} int main()
{
scanf("%d",&test);
while(test--)
{
dot gx;
scanf("%d%d",&n,&m);
rep(i,0,n-1) scanf("%s",map[i]);
queue<dot> q;
while(!q.empty()) q.pop();
ms(vis,0);
rep(i,0,n-1) rep(j,0,m-1)
{dot F;
if(map[i][j]=='J') gx.x=i,gx.y=j,gx.isfire=0,gx.time=0;
else if(map[i][j]=='F') vis[i][j]=1,F.x=i,F.y=j,F.isfire=1,F.time=0,q.push(F);
else if(map[i][j]=='#') vis[i][j]=1;
}
q.push(gx);
int ret=0;
while(!q.empty())
{
dot next;
gx=q.front(),q.pop();
rep(i,0,3)
{
next.x=gx.x+dx[i];
next.y=gx.y+dy[i];
next.isfire=gx.isfire;//BFS的时候注意"火势蔓延",相邻格子火势是相通的;
next.time=gx.time+1;
if(in(next))
{
if(!vis[next.x][next.y]) vis[next.x][next.y]=1,q.push(next);
}
else if(next.isfire==0)
{
ret=next.time;//记录当前答案;
break;
}
}
if(ret>0)break;
}
if(!ret) std::puts("IMPOSSIBLE");
else printf("%d\n",ret);
}
//A;
return 0;
}
搜索(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简单解析 与树的遍历类似,图的遍历要求从某一点出发,每 ...
- 【数据结构与算法Python版学习笔记】图——词梯问题 广度优先搜索 BFS
词梯Word Ladder问题 要求是相邻两个单词之间差异只能是1个字母,如FOOL变SAGE: FOOL >> POOL >> POLL >> POLE > ...
- 广度优先搜索 BFS 学习笔记
广度优先搜索 BFS 学习笔记 引入 广搜是图论中的基础算法之一,属于一种盲目搜寻方法. 广搜需要使用队列来实现,分以下几步: 将起点插入队尾: 取队首 \(u\),如果 $u\to v $ 有一条路 ...
- 广度优先搜索(BFS)
定义 维基百科:https://en.wikipedia.org/wiki/Breadth-first_search 给定图G=(V,E)和一个可识别的源结点s,广度优先搜索对图G中的边进行系统性的探 ...
- HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))
Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...
随机推荐
- docker 容器开启ssh服务
ssh服务安装 安装ssh服务 #yum install openssh-server -y 安装passwd(修改密码需要) #yum install passwd -y 修改sshd_config ...
- opencv 图像修复函数
void cv::inpaint( const Mat& src, const Mat& mask, Mat& dst, double inpaintRange, int fl ...
- python-操作hive
python访问hive2 HiveServer2为客户端在远程执行hive查询提供了接口,通过Thrift RPC来实现,还提供了多用户并发和认证功能.目前使用python的用户可以通过pyhs2这 ...
- BOW
bag of words(NLP): 最初的Bag of words,也叫做"词袋",在信息检索中,Bag of words model假定对于一个文本,忽略其词序和语法,句法,将 ...
- Socket.io在线聊天室
从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...
- C# 第三方控件 下面的Item不显示了
当高版本的第三方版本 替换成低版本的第三方后,item,不显示了之后,请试着再次在这基础上添加一个Item,观察这个Item和原来已经在的却不显示的Item的区别在哪里.然后去源程序正常文件哪里 将这 ...
- ubuntu, Debian, CentOS
ubuntu源自debian,内核很多文档都还是debian的字样,稳定性逐渐增强,基本满足日常开发. debian的核心稳定,性能强劲. centos的内核版本低,安全性高. 选择Debian是因为 ...
- glib源码安装使用方法
glib库是GTK+和GNOME工程的基础底层核心程序库,是一个综合用途的实用的轻量级的C程序库,它提供C语言的常用的数据结构的定义.相关的处理函数,有趣而实用的宏,可移植的封装和一些运行时机能,如事 ...
- 《Pointers On C》读书笔记(第四章 语句)
1.空语句只包含一个分号,它本身并不执行任何任务,其适用的场合是语法要求出现一条完整的语句,但并不需要它执行任何任务. 2.C语言中并不存在专门的“赋值语句”,赋值就是一种操作,在表达式内进行.通过在 ...
- HTML5速查表
HTML5速查表 标签 描述 版本 属性 <!--...--> 定义注释 4 / 5 none <!DOCTYPE> 定义文档类型 4 / 5 none <a> 定 ...