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;
}
目录
相关文章
|
6月前
|
NoSQL Linux 编译器
linux安装 redis-7.4.8.tar.gz 详细步骤(源码编译、配置、启动)
本指南详解 Redis 7.4.8 在 CentOS 环境下的源码编译安装全流程:从 GCC 环境配置、解压编译(`make && make install`),到配置文件部署、关键参数调优(后台运行、远程访问、密码认证等),最后启动验证,一步到位完成生产级 Redis 部署。
|
JSON 供应链 数据挖掘
VVIC(搜款网)商品 API 开发者详解与使用指南
VVIC(搜款网)是中国知名服装批发平台,提供丰富的货源和供应链服务。其商品API支持开发者通过编程获取商品数据,适用于电商集成、数据分析、选品工具等场景。本文详解API接入流程、核心接口、调用示例及常见问题解决方案,助力高效开发。
|
设计模式 SQL Java
一探到底!Spring团队使用的那些设计模式,快来看看你用过哪几个
该文章主要介绍了Spring框架中使用的设计模式,并列举了一些常见的设计模式及其在Spring框架中的应用。
一探到底!Spring团队使用的那些设计模式,快来看看你用过哪几个
|
人工智能 自然语言处理 计算机视觉
Janus-Pro:DeepSeek 开源的多模态模型,支持图像理解和生成
Janus-Pro是DeepSeek推出的一款开源多模态AI模型,支持图像理解和生成,提供1B和7B两种规模,适配多元应用场景。通过改进的训练策略、扩展的数据集和更大规模的模型,显著提升了文本到图像的生成能力和指令跟随性能。
3466 20
Janus-Pro:DeepSeek 开源的多模态模型,支持图像理解和生成
|
机器学习/深度学习 并行计算 算法框架/工具
Anaconda+Cuda+Cudnn+Pytorch(GPU版)+Pycharm+Win11深度学习环境配置
Anaconda+Cuda+Cudnn+Pytorch(GPU版)+Pycharm+Win11深度学习环境配置
|
存储 编译器 C++
C++ 空类的艺术:理解空类的用法与实现
C++ 空类的艺术:理解空类的用法与实现
800 0
|
Arthas 监控 Java
【Arthas】初识Arthas,安装使用
【Arthas】初识Arthas,安装使用
1158 0
【Arthas】初识Arthas,安装使用
|
Android开发
Android studio开发APP时设置更改启动时的主界面程序
Android studio开发APP时设置更改启动时的主界面程序
1896 0
Android studio开发APP时设置更改启动时的主界面程序
|
存储 网络协议 Java
10分钟让你掌握Linux常用命令
1、目录操作。2、文件操作。3、文件内容操作。4、压缩和解压缩。5、日志查看。6、Linux下文件的详细信息。7、常用的docker容器的命令。8、其他命令。
1224 0
10分钟让你掌握Linux常用命令
|
JavaScript 小程序 前端开发
vue实现微信小程序,代码实例以及如何调试
@[TOC](目录) 要调试 Vue 实现的微信小程序,您需要使用微信开发者工具进行调试。以下是一个简单的 Vue 实现的微信小程序的代码示例,以及如何使用微信开发者工具进行调试的步骤。 # 1. 首先,确保已经安装了 Vue CLI,然后使用 Vue CLI 创建一个新的 Vue 项目: ``` npm install -g @vue/cli vue create my-wechat-app cd my-wechat-app ``` # 2. 在项目中安装微信小程序开发依赖: ``` npm install wechat-jsapi wechat-devtools ``` #
1826 0