[kuangbin]专题一 简单搜索 Fire! UVA - 11624【BFS】

本文介绍了一个迷宫逃脱问题的算法解决方案。通过使用宽度优先搜索(BFS)分别对火势蔓延和人物移动进行模拟,判断人物是否能在火势蔓延到其位置前成功逃离迷宫。文章详细解释了算法流程,包括如何初始化迷宫地图、设置边界条件以及实现火势和人物的移动逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【题目描述】
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.
乔在迷宫里工作。不幸的是,迷宫的部分区域着火了,迷宫的主人忽略了制定逃生计划。帮乔摆脱迷宫。考虑到乔在迷宫中的位置以及迷宫中哪些方格着火了,你必须确定乔是否能在火到达他之前走出迷宫,以及他能以多快的速度完成这一任务。乔和火每分钟移动一个正方形,垂直或水平移动(不是对角线)。火势从每个着火的方格向四面八方蔓延。乔可以从迷宫边缘的任何正方形上走出迷宫乔和火都不能进入被墙占据的方格。

【输入】
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.
第一行包括一个整数,即后面的测试数据个数。每个测试用例的第一行包含两个整数R和C,用空格分隔(1 ≤ R, C ≤ 1000)。以下R行每一行都包含C个字符,并且每个字符都是下列之一:
• #, 墙
• ., 可通行的方格
• J, 乔在迷宫中的初始位置,这是一个可以通行的正方形
• F, 着火的方格

【输出】
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.
对于每个测试用例,如果Joe不能在火到达之前离开迷宫,输出一行“不可能”,或者一个整数,给出Joe可以安全地在几分钟内安全离开迷宫的最早时间。

【样例输入】
2
4 4
####
#JF#
#…#
#…#
3 3
###
#J.
#.F

【样例输出】
3
IMPOSSIBLE

题目链接:https://cn.vjudge.net/problem/UVA-11624

先对火进行bfs,记录到可行位置的最短蔓延时间,再对人进行bfs,判断能否在火到达之前离开

代码如下:

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
static const int MAXN=1000;
static const int dx[]={0,0,1,-1},dy[]={1,-1,0,0};
char mp[MAXN+10][MAXN+10];
int fire[MAXN+10][MAXN+10];
bool vis[MAXN+10][MAXN+10];
struct Node{
    int x,y;
    int step;
};
int R,C;
void fire_bfs(Node node)
{
    queue<Node> Q;
    fire[node.x][node.y]=0;
    Q.push(node);
    while(!Q.empty())
    {
        Node now=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            Node next;
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            next.step=now.step+1;
            if(1<=next.x && next.x<=R && 1<=next.y && next.y<=C && (mp[next.x][next.y]=='.' || mp[next.x][next.y]=='J' ) && next.step<fire[next.x][next.y])
            {
                fire[next.x][next.y]=next.step;
                Q.push(next);
            }
        }
    }
}
void Joe_bfs(Node node)
{
    queue<Node> Q;
    vis[node.x][node.y]=true;
    Q.push(node);
    while(!Q.empty())
    {
        Node now=Q.front();
        Q.pop();
        if(now.x==1 || now.x==R || now.y==1 || now.y==C)
        {
            cout<<now.step+1<<endl;
            return;
        }
        for(int i=0;i<4;i++)
        {
            Node next;
            next.x=now.x+dx[i];
            next.y=now.y+dy[i];
            next.step=now.step+1;
            if(1<=next.x && next.x<=R && 1<=next.y && next.y<=C && mp[next.x][next.y]!='#' && !vis[next.x][next.y] && next.step<fire[next.x][next.y])
            {
                vis[next.x][next.y]=true;
                Q.push(next);
            }
        }
    }
    cout<<"IMPOSSIBLE"<<endl;
    return;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        cin>>R>>C;
        for(int i=1;i<=R;i++)
            for(int j=1;j<=C;j++)
                cin>>mp[i][j];
        memset(fire,0x3f,sizeof(fire));
        for(int i=1;i<=R;i++)
            for(int j=1;j<=C;j++)
                if(mp[i][j]=='F')
                    fire_bfs(Node{i,j,0});
        memset(vis,false,sizeof(vis));
        for(int i=1;i<=R;i++)
            for(int j=1;j<=C;j++)
                if(mp[i][j]=='J')
                    Joe_bfs(Node{i,j,0});
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值