搜索进阶 Fire!

题目链接

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

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <limits>
#include <queue>
#include <stack>

using namespace std;

#define N 1100
#define INF 0xfffffff
#define PI acos (-1.0)
#define EPS 1e-8

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

struct node
{
    int x, y, t;
}J, F[N];

int n, m, k, vis1[N][N], vis[N][N];
char g[N][N];

void BFS1 ();
void BFS2 ();

int main ()
{
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> m;
        k = 0;
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<m; j++)
            {
                cin >> g[i][j];
                if (g[i][j] == 'J')
                J = (node) {i, j, 0};
                else if (g[i][j] == 'F')
                F[k++] = (node) {i, j, 0};
            }
        }
        BFS1 ();
        BFS2 ();
    }
    return 0;
}

void BFS1 ()
{
    memset (vis, 0, sizeof (vis));
    queue <node> que;
    for (int i=0; i<k; i++)
        que.push (F[i]);

    while (que.size())
    {
        node qq = que.front(); que.pop();

        if (qq.x<0 || qq.x>=n || qq.y<0 || qq.y>=m)
            return;

        for (int i=0; i<4; i++)
        {
            node q = qq;
            q.x += dir[i][0];
            q.y += dir[i][1];

            if (q.x>=0 && q.x<n && q.y>=0 && q.y<m && g[q.x][q.y]!='#' && g[q.x][q.y]!='F' && !vis[q.x][q.y])
            {
                vis[q.x][q.y] = vis[qq.x][qq.y] + 1;
                que.push(q);
            }
        }
    }
}

void BFS2 ()
{
    memset (vis1, 0, sizeof (vis1));
    queue <node> que;
    que.push (J);

    while (que.size())
    {
        J = que.front(); que.pop();

        if (J.x==0 || J.x==n-1 || J.y==0 || J.y==m-1)
        {
            cout << J.t+1 << endl;
            return;
        }

        for (int i=0; i<4; i++)
        {
            node q = J;
            q.x += dir[i][0];
            q.y += dir[i][1];
            q.t++;
            if (q.x>=0 && q.x<n && q.y>=0 && q.y<m && (vis[q.x][q.y]>q.t || (!vis[q.x][q.y] && g[q.x][q.y]!='F'))&& g[q.x][q.y]!='#' && !vis1[q.x][q.y])
            {
                vis1[q.x][q.y] = 1;
                que.push (q);
            }
        }
    }
    puts ("IMPOSSIBLE");
}

生气 生气 生气

你可能感兴趣的:(搜索进阶 Fire!)