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 (notdiagonally). The fire spreads all four directions from each square that is onfire. 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 integersR and C, separatedby spaces, with 1 <= R,C <= 1000. The followingR lines of the test caseeach contain one row of the maze. Each of these lines contains exactlyC characters,and each of these characters is one of:
#, a wall., a passable squareJ, Joe’s initial position in the maze, which is a passable squareF, 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 beforethe fire reaches him, or an integer giving the earliest time Joe can safely exit themaze, in minutes.
Output for Sample Input
3
IMPOSSIBLE
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1000+10;
const int inf = 0x3f3f3f3f;
int n, m;
char mp[N][N];
int F[N][N];
int J[N][N];
int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct node
{
int x, y;
};
void bfs_F()
{
int i, j;
queue<node>q;
node a, ne;
memset(F, -1, sizeof(F));
for(i = 0; i < n; i++){
for(j = 0; j < m; j++){
if(mp[i][j] == 'F'){
a.x = i; a.y = j;
q.push(a);
F[i][j] = 0;
}
}
}
while(!q.empty())
{
a = q.front();
q.pop();
for(i = 0; i < 4; i++){
ne.x = a.x + d[i][0];
ne.y = a.y + d[i][1];
if(ne.x < 0 || ne.x >= n || ne.y < 0 || ne.y >= m || F[ne.x][ne.y] != -1 || mp[ne.x][ne.y] == '#')
continue;
F[ne.x][ne.y] = F[a.x][a.y] + 1;
q.push(ne);
}
}
}
int bfs_J(int x, int y)
{
int i, j;
memset(J, 0, sizeof(J));
queue<node> q;
node a, ne;
a.x = x; a.y = y;
q.push(a);
while(!q.empty())
{
a = q.front();
q.pop();
if(a.x == 0 || a.y == 0 || a.x == n-1 || a.y == m-1)
return J[a.x][a.y] + 1;
for(i = 0; i < 4; i++){
//cout << 1 << endl;
ne.x = a.x + d[i][0];
ne.y = a.y + d[i][1];
if(ne.x < 0 || ne.x >= n || ne.y < 0 || ne.y >= m ||(J[a.x][a.y] + 1 >= F[ne.x][ne.y] && F[ne.x][ne.y]!= -1)|| mp[ne.x][ne.y] == '#' || J[ne.x][ne.y] != 0)
continue;
J[ne.x][ne.y] = J[a.x][a.y] + 1;
//cout << "J[" << ne.x << "][" << ne.y << "] = " << J[ne.x][ne.y] <<endl;
q.push(ne);
}
}
return -1;
}
int main()
{
int t, i, j, j1, j2;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
for(i=0; i<n; i++)
{
scanf("%s", &mp[i]);
for(j=0; j<m; j++)
{
if(mp[i][j] == 'J')
{
j1 = i; j2 = j;
}
}
}
bfs_F();
int s = bfs_J(j1, j2);
if(s == -1)
printf("IMPOSSIBLE\n");
else
printf("%d\n", s);
}
return 0;
}