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. MySQL 查询某个列中同样值的数量统计

    数据如今是这种,我想确定出type列中的news和image....甚至以后有其它值,他们分别有多少个. SELECT type, count(1) AS counts FROM material G ...

  2. 数据库历险记(三) | 缓存框架的连环炮 数据库历险记(二) | Redis 和 Mecached 到底哪个好? 数据库历险记(一) | MySQL这么好,为什么还有人用Oracle? 面对海量请求,缓存设计还应该考虑哪些问题?

    数据库历险记(三) | 缓存框架的连环炮   文章首发于微信公众号「陈树义」,专注于 Java 技术分享的社区.点击链接扫描二维码,与500位小伙伴一起共同进步.微信公众号二维码 http://p3n ...

  3. hdparm - get/set SATA/IDE device parameters

    hdparm(8) - Linux man page Name hdparm - get/set SATA/IDE device parameters Synopsis hdparm [ flags ...

  4. Android 设计模式之单例模式

    设计模式是前人在开发过程中总结的一些经验,我们在开发过程中依据实际的情况,套用合适的设计模式,能够使程序结构更加简单.利于程序的扩展和维护.但也不是没有使用设计模式的程序就不好.如简单的程序就不用了, ...

  5. 你必须了解的java内存管理机制(二)-内存分配

    前言 在上一篇文章中,我们花了较大的篇幅去介绍了JVM的运行时数据区,并且重点介绍了栈区的结构及作用,相关内容请猛戳!在本文中,我们将主要介绍对象的创建过程及在堆中的分配方式. 相关链接(注:文章讲解 ...

  6. 关于 angular cookie 设置的坑

    初识Angular,才知道掉进了这么一个各种大坑的坑. 先说下对于$cookie.put 这几个方法,只有1.4以上版本才可以用,其余低于版本请使用 $cookieStore: 下面举例下使用方法: ...

  7. oracle sql修改序列为当前序列开始

    declare   v_num integer;  last_value integer;Begin  select SEQ_TBM_ID.NEXTVAL into last_value from d ...

  8. FFmpeg编码详细流程

    FFmpeg在编码一个视频的时候的函数调用流程.为了保证结构清晰,其中仅列出了最关键的函数,剔除了其它不是特别重要的函数. 函数背景色 函数在图中以方框的形式表现出来.不同的背景色标志了该函数不同的作 ...

  9. C# 操作World生成报告

    直接上代码 using System; using System.IO; using System.Data; using Word = Microsoft.Office.Interop.Word; ...

  10. 百度之星2016资格赛D,水题

    很简单的题,主要是要用字符串哈希,把字符串处理成整数.接下来可以继续用hash,也可以像我一样用个map就搞定了. /* * Author : ben */ #include <cstdio&g ...