Fire! (double bfs + preprocessing)

Topic link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

topic:

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 IMPOSSIBL

The meaning of the question: Joe wants to escape from the burning forest. Both Joe and the fire can only move in four directions, up, down, left, and right. Joe can leave when he reaches the border. Ask the minimum number of escape steps. If you can't output IMPOSSIBL. Pit point: It is the number of steps after escaping, so +1 should be added to the number of steps to reach the boundary, there are multiple fires (because this WA has been around for a long time ==!).

Idea: first use the t array to preprocess the time when the fire reaches each place, and then perform bfs on Joe.

The code is implemented as follows:

 1 #include <queue>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int inf = 0x3f3f3f3f;
 7 int T, r, c, ans, sx, sy;
 8 char mp[1007][1007];
 9 int vis[1007][1007], t[1007][1007];
10 
11 struct node{
12     int x, y;
13     int step;
14 }nw, nxt;
15 
16 int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
17 
18 
19 void bfs1() {
20     queue<node> q;
21     for(int i = 0 ; i< r; i++) {
22         for(int j = 0; j < c; j++) {
23             if(mp[i][j] == 'F') {
24                 nw.x = i, nw.y = j;
25                 t[i][j] = 0;
26                 q.push(nw);
27             }
28         }
29     }
30     while(!q.empty()) {
31         nw = q.front(), q.pop();
32         for(int i = 0; i < 4; i++) {
33             nxt.x = nw.x + dx[i], nxt.y = nw.y + dy[i];
34             if(nxt.x >= 0 && nxt.x < r && nxt.y >= 0 && nxt.y < c && mp[nxt.x][nxt.y] != '#' && t[nxt.x][nxt.y] > t[nw.x][nw.y] + 1) {
35                 t[nxt.x][nxt.y] = t[nw.x][nw.y] + 1;
36                 q.push(nxt);
37             }
38         }
39     }
40 }
41 
42 void bfs2(int x, int y) {
43     nw.x = x, nw.y = y, nw.step = 0;
     screws[x][y] =441;
45     queue<node> q;
46     q.push(nw);
47     while(!q.empty()) {
48         nw = q.front(), q.pop();
49         if(nw.x ==0 || nw.x == r - 1 || nw.y == 0 || nw.y == c - 1) {
50             ans = nw.step + 1;
51             return;
52         }
53         for(int i = 0; i < 4; i++) {
54             nxt.x = nw.x + dx[i], nxt.y = nw.y + dy[i];
55             if(nxt.x >= 0 && nxt.x < r && nxt.y >= 0 && nxt.y <c && mp[nxt.x][nxt.y] != '#' && nw.step + 1 < t[nxt.x][nxt.y] && vis[nxt.x][nxt.y] == 0) {
56                 vis[nxt.x][nxt.y] = 1;
57                 nxt.step = nw.step + 1;
58                 q.push(nxt);
59             }
60         }
61     }
62 }
63 
64 int main() {
65     scanf("%d", &T);
66     while(T--) {
67         scanf("%d%d", &r, &c);
68         for(int i = 0; i < r; i++) {
69             scanf("%s", mp[i]);
70             for(int j = 0; j < c; j++) {
71                 if(mp[i][j] == 'J') {
72                     sx = i, sy = j;
73                 }
74             }
75         }
76         memset(vis, 0, sizeof(vis));
77         memset(t, inf, sizeof(t));
78         ans = inf;
79         bfs1();
80         bfs2(sx, sy);
81         if(ans >= inf) printf("IMPOSSIBLE\n");
82         else printf("%d\n", ans);
83     }
84     return 0;
85 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325079184&siteId=291194637