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.

#include<bits/stdc++.h>
typedef long long ll;
#define endl '\n'
using namespace std;
char mp[1010][1010];
int fire[1010][1010];
int vis[1010][1010];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
struct nm {
int x, y, temp;
};
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
int t;
cin >> t;
while (t--) {
int ans = -1;
int r, c;
cin >> r >> c;
queue<nm> f;
queue<nm> peo;
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
cin >> mp[i][j];
if (mp[i][j] == 'J') {
nm a0;
a0.x = i, a0.y = j, a0.temp = 0;
vis[i][j] = 1;
peo.push(a0);
}
if (mp[i][j] == 'F') {
nm a1;
a1.x = i, a1.y = j, a1.temp = 0;
fire[i][j] = 1;
f.push(a1);
}
}
}
while (!peo.empty()) {
nm a0 = peo.front();
peo.pop();
if (a0.x == 1 || a0.x == r || a0.y == 1 || a0.y == c) {
if (fire[a0.x][a0.y] == 0) {
ans = a0.temp + 1;
break;
}
}
while (!f.empty() && f.front().temp <= a0.temp) {
nm a1 = f.front();
f.pop();
for (int i = 0; i <= 3; i++) {
int xx = a1.x + dir[i][0];
int yy = a1.y + dir[i][1];
if (xx >= 1 && xx <= r && yy >= 1 && yy <= c && mp[xx][yy] == '.' && fire[xx][yy] == 0) {
fire[xx][yy] = 1;
nm b1;
b1.x = xx, b1.y = yy, b1.temp = a1.temp + 1;
f.push(b1);
}
}
}
for (int i = 0; i <= 3; i++) {
int xx = a0.x + dir[i][0];
int yy = a0.y + dir[i][1];
if (xx >= 1 && xx <= r && yy >= 1 && yy <= c && mp[xx][yy] == '.' && fire[xx][yy] == 0 && vis[xx][yy] == 0) {
vis[xx][yy] = 1;
nm b0;
b0.x = xx, b0.y = yy, b0.temp = a0.temp + 1;
peo.push(b0);
}
}
}
if (ans == -1)
cout << "IMPOSSIBLE" << endl;
else
cout << ans << endl;
memset(vis, 0, sizeof(vis));
memset(fire, 0, sizeof(fire));
}
return 0;
}
火灾逃生:Joe在迷宫中的生存策略,
这篇文章描述了一个关于Joe在受火势威胁的迷宫中寻找逃生路径的问题。通过分析Joe的位置、起火点以及火势扩散规则,计算他安全离开迷宫所需的最短时间,如果无法逃脱则输出IMPOSSIBLE。
2万+

被折叠的 条评论
为什么被折叠?



