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

【题目描述】
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;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
并查集是一种常用的数据结构,用于管理一个不相交集合的数据。在并查集中,每个元素都有一个父节点指向它所属的集合的代表元素。通过查找和合并操作,可以判断两个元素是否属于同一个集合,并将它们合并到同一个集合中。 在解决某些问题时,可以使用并查集进行优化。例如,在区间查询中,可以通过优化并查集的查询过程,快速找到第一个符合条件的点。 对于拆边(destroy)操作,一般的并查集无法直接实现这个功能。但是可以通过一个巧妙的方法来解决。首先,记录下所有不会被拆除的边,然后按照逆序处理这些指令。遇到拆边操作时,将该边重新加入并查集中即可。 在实现并查集时,虽然它是一种树形结构,但只需要使用数组就可以实现。可以通过将每个元素的父节点记录在数组中,来表示元素之间的关系。通过路径压缩和按秩合并等优化策略,可以提高并查集的效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [「kuangbin带你飞」专题五并查集专题题解](https://blog.csdn.net/weixin_51216553/article/details/121643742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [并查集(详细解释+完整C语言代码)](https://blog.csdn.net/weixin_54186646/article/details/124477838)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值