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

 //2017-03-01
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; struct node{
int x, y, step;
void setNode(int x, int y, int step){
this->x = x;
this->y = y;
this->step = step;
}
};
char maze[][];
int dis[][], n, m;
bool vis[][];
const int inf = 0x3f3f3f3f;
int dx[] = {, , , -};
int dy[] = {, , -, }; void bfs(int sx, int sy)
{
queue<node> q;
node tmp;
memset(vis, , sizeof(vis));
if(sx == -){
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
if(maze[i][j] == 'F')
{
tmp.setNode(i, j, );
q.push(tmp);
vis[i][j] = ;
dis[i][j] = ;
}
}else{
tmp.setNode(sx, sy, );
q.push(tmp);
vis[sx][sy] = ;
}
int x, y, step, ans;
bool fg = false;
while(!q.empty())
{
x = q.front().x;
y = q.front().y;
step = q.front().step;
q.pop();
if(maze[sx][sy]=='J'&&(x == n- || y == m- || x == || y == )){
fg = true;
ans = step+;
}
for(int i = ; i < ; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if(nx>=&&nx<n&&ny>=&&ny<m&&!vis[nx][ny]&&maze[nx][ny]!='#'){
vis[nx][ny] = ;
if(maze[sx][sy]=='J'){
if(step+ >= dis[nx][ny])continue;
else if(nx == n- || ny == m- || nx == || ny == ){
fg = true;
ans = step+;
}
}else dis[nx][ny] = min(dis[nx][ny], step+);
tmp.setNode(nx, ny, step+);
q.push(tmp);
}
}
if(fg)break;
}
if(maze[sx][sy] == 'J')
if(fg)printf("%d\n", ans);
else printf("IMPOSSIBLE\n");
} int main()
{
int T, jx, jy;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
getchar();
for(int i = ; i < n; i++){
for(int j = ; j < m; j++){
scanf("%c", &maze[i][j]);
if(maze[i][j] == 'J'){
jx = i; jy = j;
}
}
getchar();
}
memset(dis, inf, sizeof(dis));
bfs(-, -);
bfs(jx, jy);
} return ;
}

最新文章

  1. Redis常用命令
  2. 【sql】之查询昨天的记录
  3. IP_TOS选项
  4. 用CorelDRAW勾画对象轮廓图的方法
  5. bzoj 4006 [JLOI2015]管道连接(斯坦纳树+状压DP)
  6. [AngularJS] ui-router: Abstract States
  7. jsp关于include html、jsp等文件出现乱码问题的解决方案
  8. (转)OpenVPN使用HTTP代理连接服务器
  9. htm、html、shtml区别
  10. MySQL 列子查询及 IN、ANY、SOME 和 ALL 操作符的使用
  11. MCS-51系列和80C51系列单片机是否相同
  12. hdu1151Air Raid
  13. 带你深入理解STL之List容器
  14. Flink从入门到放弃(入门篇1)-Flink是什么
  15. JavaScript类型判断详解(Object.prototype.toString.call()方法进行数据类型的可靠判断)
  16. What is a Back Order
  17. dijkstra基础
  18. 单细胞测序技术(single cell sequencing)
  19. ZT 链表逆序
  20. tensorflow-base_operations

热门文章

  1. Code Chef GEOCHEAT(凸包+旋转卡壳+随机化)
  2. Smarty的原理_面试
  3. php curl 伪造IP来源的实例代码
  4. jvm内存结构(二)(栈的变化,机器指令的格式/执行模式)
  5. Swift 里字符串(九)UTF16View
  6. 字蛛fontSpider的使用
  7. POJ 2385
  8. Ubuntu18.04 Redis主从复制
  9. rabbitmq实现一台服务器同时给所有的consumer发送消息(tp框架)(第四篇)
  10. [转]SQL的主键和外键约束