UVA11624 Fire! 两次 BFS(BFS初级的综合应用)

ire!


Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze.


Given Joe's location in the maze and which squares of themaze are on fire, you must determine whether Joe can exit the maze before thefire 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 fourdirections from each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the fire may enter asquare that is occupied by a wall.


Input Specification


The first line of input contains a single integer, thenumber of test cases to follow. The first line of each test case contains thetwo integers R and C, separated byspaces, with 1 <= R,C <= 1000. Thefollowing R lines of the test case each contain one row of themaze. Each of these lines contains exactly C characters, andeach of these characters is one of:


·  #, a wall


·  ., a passable square


·  J, Joe's initialposition in the maze, which is a passable square


·  F, a square that is onfire


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 cannotexit the maze before the fire reaches him, or an integer giving the earliesttime Joe can safely exit the maze, in minutes.


Output for SampleInput


3


IMPOSSIBLE

题意:在一个迷宫中有多个火源,火源每秒可以移动到相邻的上下左右4个位置,但是不可以移动到有障碍的位置,人可以每秒移动到其他的格子,但是也无法通过有障碍的格子,问最少多少时间可以到达迷宫的边界,如果不可以到达输出不可能

思路:先得到火势蔓延到其他格子的时间,然后人只要在火势蔓延到该格子之前时间到达该格子即可 BFS

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN=1010;
int n,m;
char g[MAXN][MAXN];
queue<pair<int,int> >q;
int a[MAXN][MAXN];
int b[MAXN][MAXN];
int mov[][2]={{0,1},{0,-1},{1,0},{-1,0}};
void bfs1()
{
    memset(a,-1,sizeof(a));
    while(!q.empty())q.pop();
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(g[i][j]=='F')
            {
                a[i][j]=0;
                q.push(make_pair(i,j));
            }
    while(!q.empty())
    {
        pair<int,int> tmp=q.front();
        q.pop();
        int x=tmp.first;
        int y=tmp.second;
        for(int i=0;i<4;i++)
        {
            int tx=x+mov[i][0];
            int ty=y+mov[i][1];
            if(tx<0||ty<0||tx>=n||ty>=m)continue;
            if(a[tx][ty]!=-1)continue;
            if(g[tx][ty]=='#')continue;
            a[tx][ty]=a[x][y]+1;
            q.push(make_pair(tx,ty));
        }
    }
}

int bfs2()
{
    memset(b,-1,sizeof(b));
    while(!q.empty())q.pop();
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(g[i][j]=='J')
            {
                q.push(make_pair(i,j));
                b[i][j]=0;
            }
    while( !q.empty() )
    {
        pair<int,int> tmp=q.front();
        q.pop();
        int x=tmp.first;
        int y=tmp.second;
        if(x==0 || y==0 || x==n-1 || y==m-1)
            return b[x][y]+1;
        for(int i=0;i<4;i++)
        {
            int tx=x+mov[i][0];
            int ty=y+mov[i][1];
            if(tx<0||ty<0||tx>=n||ty>=m)continue;
            if(b[tx][ty]!=-1)continue;
            if(g[tx][ty]=='#')continue;
            if(a[tx][ty]!=-1 && b[x][y]+1>=a[tx][ty])continue;
            b[tx][ty]=b[x][y]+1;
            q.push(make_pair(tx,ty));
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
            scanf("%s",g[i]);
        bfs1();
        int ans=bfs2();
        if(ans==-1)printf("IMPOSSIBLE\n");
        else printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/deepseazbw/article/details/80744410