2012-12-26 20:31:37Morris
[UVA][bfs] 11624 - Fire!
Problem B: 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 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 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
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 before the fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.Output for Sample Input
3 IMPOSSIBLE
我忘記也有火到達不了的地方,但是為了節省空間,我忘了我的 F[i][j] 是 0,導致判斷錯誤。
#include <stdio.h>
#include <queue>
using namespace std;
char g[1010][1010];
int F[1010][1010], J[1010][1010];
int n, m, dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
struct pt {
int x, y;
};
pt pf, pj;
queue<pt> Q;
void sol() {
int i, j;
while(!Q.empty()) Q.pop();
pt tn, tx;
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
if(g[i][j] == 'F') {
pf.x = i, pf.y = j;
Q.push(pf);
F[pf.x][pf.y] = 1;
}
while(!Q.empty()) {
tn = Q.front();
Q.pop();
for(i = 0; i < 4; i++) {
tx.x = tn.x+dir[i][0];
tx.y = tn.y+dir[i][1];
if(tx.x < 0 || tx.x >= n || tx.y < 0 || tx.y >= m)
continue;
if(g[tx.x][tx.y] == '#')
continue;
if(F[tx.x][tx.y] == 0) {
F[tx.x][tx.y] = F[tn.x][tn.y]+1;
Q.push(tx);
}
}
}
Q.push(pj), J[pj.x][pj.y] = 1;
while(!Q.empty()) {
tn = Q.front();
Q.pop();
if(tn.x == 0 || tn.y == 0 || tn.x == n-1 || tn.y == m-1) {
printf("%d\n", J[tn.x][tn.y]);
return;
}
for(i = 0; i < 4; i++) {
tx.x = tn.x+dir[i][0];
tx.y = tn.y+dir[i][1];
if(tx.x < 0 || tx.x >= n || tx.y < 0 || tx.y >= m)
continue;
if(g[tx.x][tx.y] == '#')
continue;
if(J[tx.x][tx.y] == 0 && (J[tn.x][tn.y]+1 < F[tx.x][tx.y] || F[tx.x][tx.y] == 0)) {
J[tx.x][tx.y] = J[tn.x][tn.y]+1;
Q.push(tx);
}
}
}
puts("IMPOSSIBLE");
}
int main() {
int t, i, j;
scanf("%d", &t);
while(t--) {
scanf("%d %d", &n, &m);
for(i = 0; i < n; i++) {
scanf("%s", g[i]);
for(j = 0; j < m; j++) {
if(g[i][j] == 'J')
pj.x = i, pj.y = j;
F[i][j] = 0, J[i][j] = 0;
}
}
sol();
}
return 0;
}