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. visual c++ 2013进行MySQL编程(ODBC) -- (一) 套装安装

    最近写了有些技术类文章了,只因为最近研究多了些东西,有一些项目用到了,所以自己记录一下,怕自己忘记,如果有哪位同学有自己的见解,可以邮件或者回复,技术类的探讨,不管对否,都是欢迎的. 操作之前,必须安 ...

  2. Foundation框架 - NSException类

    NSException类 WBStudentManager.h #import <Foundation/Foundation.h> NSString* const NameInvalidE ...

  3. jquery easyui 全部图标

    所有的图标在 jquery-easyui-1.2.6\themes\icons 目录下, 在icon.css定义的如何引用 jquery-easyui-1.2.6/themes/icon.css .i ...

  4. 关于Swiper(概念)

    Swiper 是一款免费以及轻量级的移动设备触控滑块的js框架,使用硬件加速过渡(如果该设备支持的话). 主要使用于移动端的网站.移动web apps,native apps和hybrid apps. ...

  5. (利用DOM)在新打开的页面点击关闭当前浏览器窗口

    1.在开发过程中我们前端的用户体验中有时候会要求点击一个按钮,关闭当前浏览器窗口,用HTML DOM就可做到 2.注意:本次写法要求在新窗口中关闭.target="_blank" ...

  6. WPF01(xaml)

    XAML:(转自http://www.cnblogs.com/huangxincheng/archive/2012/06/17/2552511.html) <Window x:Class=&qu ...

  7. make mrproper及mrproper的含义

    Linux下面去编译项目之前,一般常会用make mrproper去先删除之前编译所生成的文件和配置文件,备份文件等,其中,mrproper和distclean,clean之间的区别,Linux内核源 ...

  8. vim 参数文件配置

    下面是我配置的遇到问题不能修改配置文件时的解决方案 1 /usr/share/vim/vimrc 2 这个是系统型的vimrc配置文件,为了保证vim的正常使用,一般并不会修改这个文件, 而是应该在你 ...

  9. MVC入门——详细页

    添加Action ShowDetail using System; using System.Collections.Generic; using System.Linq; using System. ...

  10. 如果在 Code First 模式下使用,则使用 T4 模板为 Database First 和 Model First

    web.config里的链接字符串最好和app.config里相同,因为ef的链接字符串需要一些特殊的参数