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.

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

题意:Joe在一个迷宫里,迷宫有的地方是着火的,火是会蔓延,Joe和火都只能上下左右移动,Joe到达迷宫边界就已经算成功,求最短步数

思路:迷宫!!bfs!!迷宫的不可通行的地方是在实时更新的,麻烦的点就在这。

我看了参考了一下网上的,创建一个二维数组来存火到达某位置的最短时间是多少,然后人再bfs的时候进行判断。看代码

#include<iostream>
#include<string.h>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
int n,m,a,b;
char Map[1010][1010];//地图 
bool vis[1010][1010];//人是否走过 
int dx[] = {-1,1,0,0},dy[] = {0,0,-1,1};//上下左右 
int df[1010][1010];//记录火到达每个可到达的地方所需要的时间 初始值为-1 
struct que
{
    int x,y,t;
    que(int xx,int yy,int tt):x(xx),y(yy),t(tt){}
};
bool check(int x,int y)
{
    if(x<0||x>=n||y<0||y>=m) return false;//越界 
    if(Map[x][y]=='F'||Map[x][y]=='#') return false;//火或者墙 
    
    return true;
}
void bfs_f()
{
    queue<que> q;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(Map[i][j]=='F')//先把火的位置压入队列 
            {
                df[i][j] = 0;//到原地记为0 
                q.push(que(i,j,0));
            }
        }
    }
    while(!q.empty())
    {
        que u = q.front();
        q.pop();
        
        for(int i=0;i<4;i++)
        {
            int xx = u.x+dx[i];
            int yy = u.y+dy[i];
            
            if(!check(xx,yy)||df[xx][yy]!=-1) continue;//非法 
            
            df[xx][yy] = u.t+1;
            q.push(que(xx,yy,df[xx][yy]));
        }
    }
}
void bfs()
{
    queue<que> q;
    q.push(que(a,b,0));
    vis[a][b] = true;
    
    while(!q.empty())
    {
        que u = q.front();
        q.pop();
        
        if(u.x==0||u.x==n-1||u.y==0||u.y==m-1)//到达边界 
        {
            printf("%d\n",u.t+1);
            return;
        }
        for(int i=0;i<4;i++)
        {
            int xx = u.x+dx[i];
            int yy = u.y+dy[i];
            
            if(!check(xx,yy)||vis[xx][yy]) continue;//非法 
            if(u.t+1>=df[xx][yy]&&df[xx][yy]!=-1) continue;//df[xx][yy]==-1是火不能到达的地方,就是这个地方卡了很久很久,具体原因看文章最下面 
        
            vis[xx][yy] = true;
            q.push(que(xx,yy,u.t+1));
        }
    }
    printf("IMPOSSIBLE\n");
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        memset(vis,false,sizeof(vis));
        memset(df,-1,sizeof(df));//初始化 
        
        for(int i=0;i<n;i++)
        {
            scanf("%s",Map[i]);
            for(int j=0;j<m;j++)
            {
                if(Map[i][j]=='J')//记录起点 
                {
                    a = i;
                    b = j;
                }
            }
        }
        bfs_f();//先对火bfs,把火到达每个能到达的地方的最短时间求出    
        bfs();//人bfs 
    }
    return 0;
}
目录
相关文章
ResizeObserver loop completed with undelivered notifications
ResizeObserver loop completed with undelivered notifications
|
6月前
|
分布式计算 大数据 API
|
内存技术
Egret的TimerEvent.TIMER和Event.ENTER_FRAME的区别
Egret的TimerEvent.TIMER和Event.ENTER_FRAME的区别
73 0
|
图形学
Unity【Timeline】- 使用Signal Track添加事件
Unity【Timeline】- 使用Signal Track添加事件
1284 0
Unity【Timeline】- 使用Signal Track添加事件
|
JavaScript 前端开发