题目

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
IMPOSSIBLE

题解

BFS问题
只是迷宫的地图是动态的,可以在拓展结点的同时,判断是否需要更新迷宫地形,然后采用另一个 BFS 来更新火焰的位置
需要注意的是 火焰起点不止一个

其他就是标准的模板

代码

/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

typedef long long LL;

const int INF = 0x7FFFFFFF;
const double eps = 1e-10;

const int maxn = 1005;

const int delta[4] = {1,-1,0,0};

char Map[maxn][maxn];
bool vis[maxn][maxn];
bool fvis[maxn][maxn];
int n,m;

struct Node {
    int x,y,dis;
    Node(int x,int y,int dis) {
        this->x = x;
        this->y = y;
        this->dis = dis;
    }
};

queue<Node> Q;
queue<Node> Qf;

void fire(int d) {
    while(!Qf.empty()) {
        int x = Qf.front().x;
        int y = Qf.front().y;
        int dis = Qf.front().dis;

        if(dis > d) {
            break;
        } else {
            Qf.pop();
        }

        for(int i = 0;i <= 3;i++) {
            int xx = x + delta[i];
            int yy = y + delta[3 - i];

            if(xx<1 || xx>n || yy<1 || yy>m)
                continue;
            if(Map[xx][yy] == '#')
                continue;
            if(fvis[xx][yy])
                continue;

            fvis[xx][yy] = true;
            Map[xx][yy] = 'F';
            
            Qf.push(Node(xx,yy,dis + 1));
        }
    }
}

void Do() {
    int jx,jy;

    cin >> n >> m;

    memset(vis,false,sizeof(vis));
    memset(fvis,false,sizeof(fvis));
    while(!Q.empty())
        Q.pop();
    while(!Qf.empty())
        Qf.pop();

    for(int i = 1;i <= n;i++) {
        for(int j = 1;j <= m;j++) {
            cin >> Map[i][j];

            if(Map[i][j] == 'J')
                jx = i,jy = j;
            if(Map[i][j] == 'F') {
                Qf.push(Node(i,j,0));
                fvis[i][j] = true;
            }
        }
    }
    Map[jx][jy] = '.';


    int ans = -1;

    Q.push(Node(jx,jy,0));
    vis[jx][jy] = true;

    int lastdis = -1;

    while(!Q.empty()) {
        int x = Q.front().x;
        int y = Q.front().y;
        int dis = Q.front().dis;
        Q.pop();

        if(dis != lastdis) {
            fire(dis);
            lastdis = dis;
        }

        for(int i = 0;i <= 3;i++) {
            int xx = x + delta[i];
            int yy = y + delta[3 - i];
            int dist = dis + 1;

            if(xx<1 || xx>n || yy<1 || yy>m) {
                ans = dist;
                break;
            }

            if(Map[xx][yy] == '.' && vis[xx][yy] == false) {
                vis[xx][yy] = true;
                Q.push(Node(xx,yy,dist));
            }

        }
        if(ans != -1)
            break;
    }

    if(ans == -1)
        cout << "IMPOSSIBLE" << endl;
    else
        cout << ans << endl;
}

int main() {
    cin.tie(0);
    cin.sync_with_stdio(false);

    int T;
    cin >> T;
    while(T--)
        Do();

    return 0;
}