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
两次bfs,第一次用于确定着火的时间
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Node{
int a,b,s;
Node(){}
Node(int ca,int cb,int cs)
{
a=ca,b=cb,s=cs;
}
};
char mp[MAXN][MAXN];
int vis[MAXN][MAXN];
int Fire[MAXN][MAXN];
int n,m;
int yJ,xJ;
int dy[]={,,,-};
int dx[]={,,-,};
void bfsF()
{
memset(vis,,sizeof(vis));
queue<Node> que; for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
Fire[i][j]=INF;
if(mp[i][j]=='F')
{
que.push(Node(i,j,));
vis[i][j]=;
}
} while(!que.empty())
{
Node now=que.front();que.pop();
Fire[now.a][now.b]=now.s;
for(int i=;i<;i++)
{
int ny=now.a+dy[i];
int nx=now.b+dx[i];
if(<=ny&&ny<n&&<=nx&&nx<m&&mp[ny][nx]!='#'&&!vis[ny][nx])
{
vis[ny][nx]=;
que.push(Node(ny,nx,now.s+));
} }
}
}
void bfsJ()
{
memset(vis,,sizeof(vis));
queue<Node> que;
que.push(Node(yJ,xJ,));
vis[yJ][xJ]=;
while(!que.empty())
{
Node now=que.front();que.pop();
if(now.a==||now.a==n-||now.b==||now.b==m-)
{
printf("%d\n",now.s+);
return ;
}
for(int i=;i<;i++)
{
int ny=now.a+dy[i];
int nx=now.b+dx[i];
if(<=ny&&ny<n&&<=nx&&nx<m&&!vis[ny][nx]&&mp[ny][nx]!='#'&&now.s+<Fire[ny][nx])
{
vis[ny][nx]=;
que.push(Node(ny,nx,now.s+));
}
}
}
printf("IMPOSSIBLE\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%*c");
for(int j=;j<m;j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='J')
{
yJ=i;
xJ=j;
}
}
}
bfsF();
bfsJ();
} return ;
}

UVA11624(bfs最短路)的更多相关文章

  1. POJ 2251 Dungeon Master (BFS最短路)

    三维空间里BFS最短路 #include <iostream> #include <cstdio> #include <cstring> #include < ...

  2. 【bzoj5049】[Lydsy九月月赛]导航系统 并查集+双向BFS最短路

    题目描述 给你一张 $n$ 个点 $m$ 条边的随机图,边权为1.$k$ 次询问两点间最短路,不连通则输出-1. 输入 第一行包含3个正整数n,m,k(2<=n<=100000,1< ...

  3. 【bzoj1189】[HNOI2007]紧急疏散evacuate BFS最短路+动态加边网络流

    题目描述 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一扇门,人们可以 ...

  4. BZOJ 1195 [HNOI2006]最短母串 (Trie图+状压+bfs最短路)

    BZOJ1195 LOJ10061 题目大意:给你$n$个模式串,求一个最短且字典序最小的文本串并输出这个串,$n<=12,len<=50$ 首先对所有模式串构造$Trie$图,$Trie ...

  5. UVa 1600 Patrol Robot (BFS最短路 && 略不一样的vis标记)

    题意 : 机器人要从一个m * n 网格的左上角(1,1) 走到右下角(m, n).网格中的一些格子是空地(用0表示),其他格子是障碍(用1表示).机器人每次可以往4个方向走一格,但不能连续地穿越k( ...

  6. HDU2433 BFS最短路

    Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. 【USACO 2.4】Overfencing(bfs最短路)

    H行W列的迷宫,用2*H+1行的字符串表示,每行最多有2*W+1个字符,省略每行后面的空格.迷宫的边界上有且仅有两个出口,求每个点出发到出口的最短路. +-+-+-+-+-+ | | +-+ +-+ ...

  8. HDU 1548 A strange lift (bfs / 最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Time Limit: 2000/1000 MS (Java/Ot ...

  9. HDU 4634 Swipe Bo 状态压缩+BFS最短路

    将起始点.终点和钥匙统一编号,预处理: 1.起始点到所有钥匙+终点的最短路 2.所有钥匙之间两两的最短路 3.所有钥匙到终点的最短路 将起始点和所有钥匙四方向出发设为起点BFS一遍,求出它到任意点任意 ...

随机推荐

  1. vue2.0 + vux (六)NewsList 资讯页 及 NewsDetail 资讯详情页

    设置代理,避免出现跨域问题 /*设置代理,避免出现跨域问题*/ proxyTable: { '/api':{ target: 'https://www.oschina.net/action/apiv2 ...

  2. 使用canvas 的api 实现 图片的显示 及 压缩

    在移动端压缩图片并且上传主要用到filereader.canvas 以及 formdata 这三个h5的api.逻辑并不难.整个过程就是: (1)用户使用input file上传图片的时候,用file ...

  3. python(23)- 面向对象简单介绍

    面向概述 面向过程:根据业务逻辑从上到下写垒代码 面向过程的设计的核心是过程,过程即解决问题的步骤, 面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西 优点:极大降低了程序的复杂 ...

  4. 爬虫基本操作、requests和BeautifulSoup

    1. 爬虫基本操作 例如舆情系统: 获取汽车之家新闻放到自己数据库里,创建自己的app,发布内容,注明来源,自己创业. URL指定内容获取到 - 发送Http请求:http://www.autohom ...

  5. sass的使用(载)

    一.什么是SASSSASS是一种CSS的开发工具,提供了许多便利的写法,大大节省了设计者的时间,使得CSS的开发,变得简单和可维护.本文总结了SASS的主要用法.我的目标是,有了这篇文章,日常的一般使 ...

  6. java 是 传值还是传址 Pass-by-value or Pass-by-reference

    原文在此,写的非常好,解答了我的疑问 http://www.javadude.com/articles/passbyvalue.htm 首先放上一段代码,我是在找寻这段代码的内部原理的时候,在stac ...

  7. Nginx下的https配置

    https: https(Secure Hypertext Transfer Protocol) 安全超文本传输协议 它是以安全为目标的http通道,即它是http的安全版.它使用安全套接字层(SSL ...

  8. SAM4E单片机之旅——7、LED闪烁之TC中断

    RTT主要用做一个全局的定时器,而且不太通用.现在尝试使用一个更为通用的定时器进行定时:定时计数器(Timer Counter, TC). TC提供了广泛的功能,主要可以分为对输入的测量,以及波形的输 ...

  9. 开源G711A/PCMA、G711U/PCMU、G726、PCM转码AAC项目EasyAACEncoder

    项目及源码地址:https://github.com/EasyDarwin/EasyAACEncoder EasyAACEncoder 是EasyDarwin开源流媒体服务团队整理.开发的一款音频转码 ...

  10. "Installing Software" has encountered a problem---pydev on ubuntu

    "Installing Software" has encountered a problem. An error occurred while collecting items ...