zoukankan      html  css  js  c++  java
  • 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
    ");
    82         else printf("%d
    ", ans);
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    c++:函数模板
    1084 外观数列
    1083 是否存在相等的差
    1082 射击比赛
    1081 检查密码
    1080 MOOC期终成绩
    1079 延迟的回文数
    1078 字符串压缩与解压
    1077 互评成绩计算
    1076 Wifi密码
  • 原文地址:https://www.cnblogs.com/Dillonh/p/8975876.html
Copyright © 2011-2022 走看看