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
|
7月前
|
分布式计算 大数据 API
fire框架
fire框架
|
7月前
|
存储 Go
D - MaratonIME in the golden moment
D - MaratonIME in the golden moment
|
前端开发
The following tasks did not complete: first Did you forget to signal async completion?
The following tasks did not complete: first Did you forget to signal async completion?
|
内存技术
Egret的TimerEvent.TIMER和Event.ENTER_FRAME的区别
Egret的TimerEvent.TIMER和Event.ENTER_FRAME的区别
77 0
|
存储 机器学习/深度学习 人工智能
PTPCG: Efficient Document-level Event Extraction via Pseudo-Trigger-aware Pruned Complete Graph论文解读
据我们所知,我们目前的方法是第一项研究在DEE中使用某些论元作为伪触发词的效果的工作,我们设计了一个指标来帮助自动选择一组伪触发词。此外,这种度量也可用于度量DEE中带标注触发词的质量。
127 1
|
机器学习/深度学习 人工智能 自然语言处理
DualCor: Event Causality Extraction with Event Argument Correlations论文解读
事件因果关系识别(ECI)是事件因果关系理解的重要任务,其目的是检测两个给定文本事件之间是否存在因果关系。然而,ECI任务忽略了关键的事件结构和因果关系组件信息
103 0
|
自然语言处理 Java 测试技术
Saliency as Evidence: Event Detection with Trigger Saliency Attribution 论文解读
事件检测(ED)是事件抽取的关键子任务,它试图识别文本中特定类型的事件触发词。尽管ED取得了重大进展,但现有方法通常遵循“一个模型适合所有类型”的方法,这种方法认为事件类型之间没有差异,通常会导致相当倾斜的性能。
79 0
|
图形学
Unity【Timeline】- 使用Signal Track添加事件
Unity【Timeline】- 使用Signal Track添加事件
1304 0
Unity【Timeline】- 使用Signal Track添加事件