版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/42275651

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.

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

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Output Specification

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.

Output for Sample Input

3
IMPOSSIBLE





//3
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<sstream>
#include<time.h>
#include<utility>
#include<malloc.h>
#define MAX 1010
using namespace std;

int n,m;
int t;

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

char p[MAX][MAX];

int check(int x, int y)
{
    if (x >= 0 && x < n && y >= 0 && y < m)
        return 1;
    return 0;
}

int a[MAX][MAX];
queue<pair<int,int> > q;

void bfs1()
{
    while (!q.empty())
        q.pop();
    memset(a,-1,sizeof(a));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            if (p[i][j] == 'F')
            {
                a[i][j] = 0;
                q.push(make_pair(i,j));
            }
        }

    while (!q.empty())
    {
        pair<int, int> tmp;
        tmp = q.front();
        q.pop();

        for (int i = 0; i < 4; i++)
        {
            int x = tmp.first + dir[i][0];
            int y = tmp.second + dir[i][1];
            if ( check(x, y) == 0 || a[x][y] != -1 || p[x][y] == '#')
                continue;
            a[x][y] = a[tmp.first][tmp.second] + 1;
            q.push(make_pair(x,y));
        }
    }
}

int b[MAX][MAX]; 

void bfs()
{
    while (!q.empty())
        q.pop();

    memset(b,-1,sizeof(b));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            if (p[i][j] == 'J')
            {
                q.push(make_pair(i, j));
                b[i][j] = 0;
                break;
            }
        }

    while (!q.empty())
    {
        pair<int,int> tmp;
        tmp = q.front();
        q.pop();

        if ( tmp.first == 0 || tmp.first == n-1 || tmp.second == 0 || tmp.second == m-1)
        {
            printf("%d\n",b[tmp.first][tmp.second] + 1);
            return;
        }

        for (int i = 0; i < 4; i++)
        {
            int x = tmp.first + dir[i][0];
            int y = tmp.second + dir[i][1];
            if (check(x, y) == 0 || b[x][y]!=-1 || p[x][y] == '#')
                continue;
            if (a[x][y] != -1 && b[tmp.first][tmp.second] + 1 >= a[x][y])
                continue;

            b[x][y] = b[tmp.first][tmp.second] + 1;
            q.push(make_pair(x, y));
        }
    }

    printf("IMPOSSIBLE\n");
    return;
}

int main()
{
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d %d",&n,&m);
        for (int i = 0; i < n; i++)
            scanf("%s",p[i]);

        bfs1();
        bfs();

    }
    return 0;
}