Fire! (双bfs+预处理)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

题目:

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

题意:Joe要逃离着火的森林,Joe和火都只能往上下左右四个方向转移,Joe到达边界即可离开,问最小逃离步数,如果不能输出IMPOSSIBL。坑点:是逃离后的步数,因而要在到达边界的步数上+1,火有多个(因为这个WA了好久==!)。

思路:先用t数组预处理出火到达每个地方的时间,然后再对Joe进行bfs即可。

代码实现如下:

 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;
44     vis[x][y] = 1;
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 }

 

转载于:https://www.cnblogs.com/Dillonh/p/8975876.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值