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. css-装饰

    css -在标签上设置style样式 background-color:#2356a1 height:48px -编写样式方法 1.标签的style属性 2.鞋子head里面,style标签中编写 - ...

  2. LeetCode(15)题解--3Sum

    https://leetcode.com/problems/3sum/ 题目: Given an array S of n integers, are there elements a, b, c i ...

  3. 【BZOJ3566】[SHOI2014]概率充电器 期望+树形DP

    [BZOJ3566][SHOI2014]概率充电器 Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器:“采用全新纳米级加工技术,实现元件与导线 ...

  4. leetcode题目解答报告(2)

    Pascal's Triangle 题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  5. 九度OJ 1117:整数奇偶排序 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3174 解决:932 题目描述: 输入10个整数,彼此以空格分隔.重新排序以后输出(也按空格分隔),要求: 1.先输出其中的奇数,并按从大到 ...

  6. HDOJ 4689 Derangement DP

    DP具体解释见: http://blog.csdn.net/liguan1/article/details/10468139 Derangement Time Limit: 7000/7000 MS ...

  7. 20170319 ABAP 生成XML文件

    方法一:ABAP 使用method方式操作XML 转自:http://www.cnblogs.com/jiangzhengjun/p/4265595.html 方法二:STRANS 转换工具;使用st ...

  8. [数据挖掘课程笔记]SLIQ算法

    1.数据结构 主要的数据结构有:1.Attribute List  2.Class List 对于数据集,每一个属性都有一个对应的Attribute List.如上图所示,每个Attribute Li ...

  9. xcode7和ios9下UIWebView不能加载网页的解决方法

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  10. [NOIP2011提高组day2]-2-聪明的质监员

    2.聪明的质监员(qc.cpp/c/pas) [问题描述] 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有 n 个矿石,从 1到 n 逐一编号,每个矿石都有自己的重量 wi 以及价 ...