Fire!(BFS)

Fire!
Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit  Status

Descriptionios

Download as PDF
 

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.ui

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.spa

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 <=  RC <= 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.
 
广度优先搜索~
 
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <queue>
  4 #include <vector>
  5 #include <utility>
  6 #include <cstdio>
  7 #include <cstring>
  8 
  9 using namespace std;
 10 
 11 struct Pos
 12 {
 13     int y, x, step;
 14     Pos(int a, int b, int c) {y = a; x = b; step = c;}
 15 };
 16 int r, c, fi, fj, ji, jj;
 17 char m[1001][1001];
 18 queue<Pos> que;
 19 int fx[1000001], fy[1000001], top, tail;
 20 
 21 const int d[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
 22 
 23 bool Judge(int i, int j)
 24 {
 25     return  i > -1 && j > -1 && i < r && j < c && m[i][j] == '.';
 26 }
 27 
 28 bool Judge2(int i, int j)
 29 {
 30     return i > -1 && j > -1 && i < r && j < c && m[i][j] != '#' && m[i][j] != 'F';
 31 }
 32 
 33 bool isExit(int i, int j)
 34 {
 35     return i == 0 || j == 0 || i == r - 1 || j == c - 1;
 36 }
 37 
 38 void fire_spread()
 39 {
 40     int y, x, yy, xx, end = tail;
 41     for(; top < end; top++){
 42         y = fy[top];
 43         x = fx[top];
 44         for(int i = 0; i < 4; i++){
 45             yy = y + d[i][0];
 46             xx = x + d[i][1];
 47             if(Judge2(yy, xx)){
 48                 m[yy][xx] = 'F';
 49                 fx[tail] = xx;
 50                 fy[tail] = yy;
 51                 tail++;
 52             }
 53         }
 54     }
 55 }
 56 
 57 int main()
 58 {
 59     int t;
 60     scanf("%d", &t);
 61     while(t--){
 62         bool ok = false;
 63         top = tail = 0;
 64         while(!que.empty()) que.pop();
 65         scanf("%d %d", &r, &c);
 66         for(int i = 0; i < r; i++){
 67             scanf("%s", m[i]);
 68             for(int j = 0; m[i][j] != '\0'; j++){
 69                 if(m[i][j] == 'J') que.push(Pos(i, j, 0));
 70                 else if(m[i][j] == 'F') fy[tail] = i, fx[tail] = j, tail++;
 71             }
 72         }
 73         int pre = 0;
 74         fire_spread();
 75         while(!que.empty()){
 76             Pos cur = que.front();
 77             que.pop();
 78             if(isExit(cur.y, cur.x)){
 79                 pre = cur.step + 1;
 80                 ok = true;
 81                 break;
 82             }
 83             if(cur.step > pre) {
 84                 fire_spread();
 85                 pre++;
 86             }
 87             for(int i = 0; i < 4; i++){
 88                 int ii = cur.y + d[i][0];
 89                 int jj = cur.x + d[i][1];
 90                 if(Judge(ii, jj)) {
 91                     m[ii][jj] = 'P';
 92                     que.push(Pos(ii, jj, cur.step + 1));
 93                 }
 94             }
 95         }
 96         if(!ok) puts("IMPOSSIBLE");
 97         else printf("%d\n", pre);
 98     }
 99     return 0;
100 }