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

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.

Output

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.

Sample Input

2

4 4

####

#JF#

#..#

#..#

3 3

###

#J.

#.F

Sample Output

3

IMPOSSIBLE

双向BFS。这题会被样例误导比较坑。。F点其实可以有多个。把J和someF分别加入两个队列,先扩展F点,把一步之内的点标记下,再扩展J,使得F可以影响J的路线。当J到达边界即逃脱,否则被#墙及F点围堵Fire。。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; char a[][];
int b[][];
int t[][]={{,},{,},{-,},{,-}}; struct Node{
int x,y,s;
}node; int main()
{
int tt,n,m,i,j;
scanf("%d",&tt);
while(tt--){
scanf("%d%d",&n,&m);
queue<Node> qj,qf;
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(i=;i<n;i++){
getchar();
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='J'){
b[i][j]=;
node.x=i;
node.y=j;
node.s=;
qj.push(node);
}
if(a[i][j]=='F'){
b[i][j]=;
node.x=i;
node.y=j;
node.s=;
qf.push(node);
}
}
}
int f=;
while(qj.size()){
int ss=qf.front().s;
while(qf.size()&&ss==qf.front().s){
for(i=;i<;i++){
int tx=qf.front().x+t[i][];
int ty=qf.front().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(a[tx][ty]=='#') continue;
if(b[tx][ty]==){
b[tx][ty]=;
node.x=tx;
node.y=ty;
node.s=qf.front().s+;
qf.push(node);
}
}
qf.pop();
}
int sss=qj.front().s;
while(qj.size()&&sss==qj.front().s){
for(i=;i<;i++){
int tx=qj.front().x+t[i][];
int ty=qj.front().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m){
f=qj.front().s+;
break;
}
if(a[tx][ty]=='#') continue;
if(b[tx][ty]==){
b[tx][ty]=;
node.x=tx;
node.y=ty;
node.s=qj.front().s+;
qj.push(node);
}
}
if(f!=) break;
qj.pop();
}
if(f!=) break;
}
if(f==) printf("IMPOSSIBLE\n");
else printf("%d\n",f);
}
return ;
}

UVA - 11624 Fire! 双向BFS追击问题的更多相关文章

  1. UVA 11624 - Fire! 图BFS

    看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...

  2. UVa 11624 Fire!(BFS)

    Fire! Time Limit: 5000MS   Memory Limit: 262144KB   64bit IO Format: %lld & %llu Description Joe ...

  3. (简单) UVA 11624 Fire! ,BFS。

    Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...

  4. UVA - 11624 Fire! 【BFS】

    题意 有一个人 有一些火 人 在每一秒 可以向 上下左右的空地走 火每秒 也会向 上下左右的空地 蔓延 求 人能不能跑出来 如果能 求最小时间 思路 有一个 坑点 火是 可能有 多处 的 样例中 只有 ...

  5. uva 11624 Fire! 【 BFS 】

    按白书上说的,先用一次bfs,求出每个点起火的时间 再bfs一次求出是否能够走出迷宫 #include<cstdio> #include<cstring> #include&l ...

  6. BFS(两点搜索) UVA 11624 Fire!

    题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...

  7. UVa 11624 Fire!(着火了!)

    UVa 11624 - Fire!(着火了!) Time limit: 1.000 seconds Description - 题目描述 Joe works in a maze. Unfortunat ...

  8. UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次

    UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...

  9. CSUOJ2031-Barareh on Fire(双向BFS)

    Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...

随机推荐

  1. bb=Discuz与 Discuz! X ,Discuz!NT区别

    没加x的,仅仅是单独的论坛. 加了x的,模块加了很多了,门户,家园,排行榜,群组,都是Discuz! X上的,而Discuz!上没有,所以说Discuz! X更加适用于建设门户网 Discuz! X ...

  2. 不为客户连接创建子进程的并发回射服务器( poll实现 )

    前言 在上文中,我使用select函数实现了不为客户连接创建子进程的并发回射服务器( 点此进入 ).但其中有个细节确实有点麻烦,那就是还得设置一个client数组用来标记select监听描述符集中被设 ...

  3. css常用总结

    1.固定一个层在页面的位置,不受滚动条影响, 属性position:fixed,如: .tbar{ height:200px;width:60px;background-color:#666;posi ...

  4. [工具]利用EasyRTSPClient工具检查摄像机RTSP流不能播放原因以及排查音视频数据无法播放问题

    出现问题 我们在做流媒体开发的过程中,进程会出现摄像机RTSP流莫名其妙无法播放的问题,而我们常用的vlc经常是直接弹出一个无法播放的提示框就完事了,没有说明出错的原因,或者在vlc的消息里面能看到日 ...

  5. EasyRTMP手机直播推送rtmp流flash无法正常播放问题

    本文转自EasyDarwin团队Kim的博客:http://blog.csdn.net/jinlong0603/article/details/52960750 问题简介 EasyRTMP是EasyD ...

  6. c# vs2010 连接access数据库

    第一次在博客园写博文,由于文采不怎么好,即使是自己很熟悉的东西,写起来也会感觉到不知从何讲起,我想写的多了就好了. 这篇文章主要是介绍怎么用c# 语言 vs2010连接access数据库的,连接字符串 ...

  7. 九度OJ 1100:最短路径 (最短路径)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4185 解决:619 题目描述: N个城市,标号从0到N-1,M条道路,第K条道路(K从0开始)的长度为2^K,求编号为0的城市到其他城市的 ...

  8. The server must be started under an unprivileged user ID to prevent

    mysql8 PostgreSQL [root@test local]# postgres -D /usr/local/pgsql/data"root" execution of ...

  9. webapp 打包

    输入您的WAP网址,技术员马上帮您封装APP! APP人工打包-智睿软件_app打包_苹果app发布_app 上架_ios 上架_封装app_网站转app_安卓发布 http://app.niuhu1 ...

  10. JS中正则匹配开头不带空格,结尾也不带空格的字符串

    在做项目的时候,要求限制SSID的长度.以及开头和结尾不能是空格. var reg = /^\S.{0,30}\S$/ "$$$  $$".match(reg);   ==> ...