搜索(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 ...
随机推荐
- HDU 5738 Eureka(极角排序)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5738 [题目大意] 给出平面中一些点,在同一直线的点可以划分为一个集合,问可以组成多少包含元素不少 ...
- poj2823_单调队列简单入门
题目链接:http://poj.org/problem?id=2823 #include<iostream> #include<cstdio> #define M 100000 ...
- 详解Spring中的CharacterEncodingFilter--forceEncoding为true在java代码中设置失效--html设置编码无效
在项目中有很多让人头疼的问题,其中,编码问题位列其一,那么在Spring框架中是如何解决从页面传来的字符串的编码问题的呢?下面我们来看看Spring框架给我们提供过滤器CharacterEncodin ...
- js完美继承代码示例
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 剑指offer 替换字符串中的空格
void replaceSpace(char *str,int length) { ) return; ; ; ; while(str[originlen]!='\0') { if(str[origi ...
- 音乐ID3 中 专辑封面解析(APIC帧)
ID3V2 中 APIC 帧标识 专辑封面.前几天 百度 谷歌 都没有找到具体的说明.有点小伤人. 最好参考 Android 中的 id3.cpp 以及一个java 开源 id3 库.找到这里的规格 ...
- selector是在文件夹drawable中进行定义的xml文件。
获取Drawable对象: Resources res = mContext.getResources(); Drawable myImage = res.getDrawable(R.drawable ...
- css3动画工具
去年,我刚刚开始学习css3时候,看到了腾讯的这个工具,引起了我对css3的兴趣. 配合着书本上的知识写了一些效果,感觉不错. http://www.f2e.name/case/css3/tools. ...
- 使用astyle格式化代码【脚本】
astyle使用基础教程 http://cppblog.com/jokes000/articles/158838.html steps: (1) apt-get install astyle 或者去主 ...
- std中map
在map中需要对位置a和b值进行交换,代码如下: auto val1 = tmpMap.at(a); auto val2 = tmpMap.at(b); tmpMap.insert(std::make ...