搜索(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 ...
随机推荐
- Linux的正常关机
Azure上的 IaaS 虚拟机可使用多种方式关闭,例如通过 Azure 管理门户.Azure Powershell cmdlet 或 CLI 工具,甚至还可以由交互式登录的用户关闭.Azure 平台 ...
- 达内TTS6.0课件oop_day01
- 采用proguard困惑android代码
当前是有些工具比方apktool,dextojar等是能够对我们android安装包进行反编译.获得源代码的.为了降低被别人破解,导致源代码泄露,程序被别人盗代替码,等等.我们须要对代码进行混淆.an ...
- 给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X
题目:给定数组A,大小为n,现给定数X,判断A中是否存在两数之和等于X 思路一: 1,先采用归并排序对这个数组排序, 2,然后寻找相邻<k,i>的两数之和sum,找到恰好sum>x的 ...
- T4模板之菜菜鸟篇
一.废话 T4(Text Template Transformation Toolkit)是微软官方在VisualStudio 2008中开始使用的代码生成引擎.在 Visual Studio 中,“ ...
- $.ajax传递字符串到后台,后台返回json对象
var mall = { MallID: $("#createId").val().trim(), MallName: $("#createName").val ...
- Asp.Net MVC 控制器
原文链接:http://www.asp.net/learn/mvc/ 这篇教程探索了ASP.NET MVC控制器(controller).控制器动作(controller action)和动作结果(a ...
- HOJ1087
Self Numbers My Tags (Edit) Source : ACM ICPC Mid-Central USA 1998 Time limit : 5 sec Memory ...
- Git - 常用技能
参考: http://wuchong.me/blog/2015/03/30/git-useful-skills/
- R与数据分析旧笔记(十四) 动态聚类:K-means
动态聚类:K-means方法 动态聚类:K-means方法 算法 选择K个点作为初始质心 将每个点指派到最近的质心,形成K个簇(聚类) 重新计算每个簇的质心 重复2-3直至质心不发生变化 kmeans ...