J - Fire! UVA - 11624

J - Fire!

UVA - 11624

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

Malcolm Sharpe, Ondřej Lhoták

AC代码

#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>

using namespace std;
const int maxn = 1050;
const int INF = 0x3f3f3f3f;
const int mod = 7;
typedef long long ll;
#define PI 3.1415927


int n, m;
int t;
int fc = 0;
int dir[4][2] = {1,0,0,1,-1,0,0,-1};
bool vis[maxn][maxn];
bool flag = false;
int ans = -1;
struct Node
{
    int x, y;
    int step;
    Node(int _x, int _y, int _s){x=_x;y=_y;step=_s;}
};
int fire[maxn][maxn];
char mp[maxn][maxn];
int Fire[maxn*maxn][2];
int sx, sy;
void bfs()
{
    queue<Node> que;
    for(int i = 0; i < fc; ++i)
    {
        fire[Fire[i][0]][Fire[i][1]] = 0;
        que.push(Node(Fire[i][0], Fire[i][1], 0));
    }
    while(que.size())
    {
        Node next = que.front();
        que.pop();
        for(int i = 0; i < 4; ++i)
        {
            int nx = next.x +dir[i][0], ny = next.y +dir[i][1];
            if(0<=nx&&nx<n&&0<=ny&&ny<m&&mp[nx][ny]!='#'&&fire[nx][ny]==-1)
            {
                fire[nx][ny] = next.step+1;
                que.push(Node(nx,ny,next.step+1));
            }
        }
    }
    que.push(Node(sx, sy, 0));
    vis[sx][sy] = true;
    while(que.size())
    {
        Node next = que.front();
        que.pop();
        if(next.x == 0|| next.y == 0||next.x == n-1 ||next.y == m-1)
        {
            ans = next.step+1;
            flag = true;
            return;
        }
        for(int i = 0; i < 4; ++i)
        {
            int nx = next.x +dir[i][0], ny = next.y +dir[i][1];
            
            if(0<=nx&&nx<n&&0<=ny&&ny<m&&mp[nx][ny]!='#'&&((fire[nx][ny]!=-1 && fire[nx][ny]>next.step+1)||fire[nx][ny]==-1)&&!vis[nx][ny])
            {
                vis[nx][ny] = true;
                que.push(Node(nx,ny,next.step+1));
            }
        }
    }
}
int main()
{
    scanf("%d", &t);
    getchar();

    while(t--)
    {
        fc = 0;
        flag = false;
        ans = -1;
        memset(vis, 0, sizeof(vis));
        memset(fire, -1, sizeof(fire));
        scanf("%d%d", &n,&m);
        getchar();
        for(int i = 0; i < n; ++i)
        {
            for(int j = 0; j < m; ++j)
            {
                mp[i][j] = getchar();
                if(mp[i][j] == 'J')
                {
                    sx = i; sy = j;
                }
                if(mp[i][j] == 'F')
                {
                    Fire[fc][0] = i; Fire[fc++][1] = j;
                }
            }
            getchar();
        }
        bfs();
        if(ans == -1)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/81325499
今日推荐