Fire 两个bfs

本文介绍了一道编程题目,涉及到一个被困在着火迷宫中的人(Joe)试图逃离的情景。Joe和火每分钟移动一格,火会向四面八方扩散,且不能通过墙壁。任务是判断Joe能否在火势到达前逃离,并给出最短逃生时间。样例输入和输出展示了具体的操作情况。
摘要由CSDN通过智能技术生成

Joe works in a maze. Unfortunately, portions of the maze have
caught on re, and the owner of the maze neglected to create a re
escape plan. Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze
are on re, you must determine whether Joe can exit the maze before
the re reaches him, and how fast he can do it.
Joe and the re each move one square per minute, vertically or
horizontally (not diagonally). The re spreads all four directions
from each square that is on re. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the re
may enter a square that is occupied by a wall.
Input
The rst line of input contains a single integer, the number of test
cases to follow. The rst 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 re
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
re 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

这道题意思一个迷宫里有个人,有一堆着火的点。每次火向四周蔓延,判断人是否能跑出这个迷宫(跑到边界就是跑出迷宫),如果能输出步数。

  说一下我的思路,很简单,把人到每个边界需要的步数算出来,
再把火到每个边界的最小步数算出来,然后for一下,看哪一个点人到边界比火快,步数是多少,就这样输出边界最小的一个值就行啦。
#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int m, n, vis[1111][1111];
int disj[1111][1111];
int disf[1111][1111];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char a[1111][1111];
struct node
{
    int x, y, step;
};
void bfs(int x, int y);
void bbfs(int x, int y);
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        memset(disj, inf, sizeof(disj));
        memset(disf, inf, sizeof(disf));
        memset(vis, 0, sizeof(vis));
        memset(a, 0, sizeof(a));
        cin >> m >> n;
        for(int i = 1; i <= m; i++)
            for(int j = 1; j <= n; j++)
                cin >> a[i][j];
        for(int i = 1; i <= m; i++)
            for(int j = 1; j <= n; j++)
            {
                if(a[i][j] == 'J')
                {
                    bfs(i, j);
                    memset(vis, 0, sizeof(vis));
                }
                else if(a[i][j] == 'F')
                {
                    bbfs(i, j);       
                    memset(vis, 0, sizeof(vis));
                }
            }
        int mmin = inf;
        for(int i = 1; i <= m; i++)
        {
            for(int j = 1; j <= n; j++)
            {
              if(i == 1 || i == m || j == 1 || j == n)
              {
              if(disj[i][j] < disf[i][j] && disj[i][j] < mmin && a[i][j]!='#')
                 mmin = disj[i][j];
              }
            }
        }
        if(mmin < inf)
        {
        cout << mmin + 1 << endl;
        }
        else cout << "IMPOSSIBLE" <<endl;
    }
}

void bfs(int x, int y)    //求人的
{
    vis[x][y] = 1;
    disj[x][y] = 0;
    queue<node>q;
    q.push((node)
    {
        x, y, 0
    });
    while(q.size())
    {
        node now = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            int xx = now.x + dx[i];
            int yy = now.y + dy[i];
            if(xx >= 1 && xx <= m && yy >=1 && yy <= n && !vis[xx][yy] && a[xx][yy] != '#')
            {
                vis[xx][yy] = 1;
                disj[xx][yy] = now.step+1;
                q.push((node)
                {
                    xx, yy, now.step+1
                });
            }
        }
    }
}

void bbfs(int x, int y)      //求火的
{
    vis[x][y] = 1;
    disf[x][y] = 0;
    queue<node>q;
    q.push((node)
    {
        x, y, 0
    });
    while(q.size())
    {
        node now = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            int xx = now.x + dx[i];
            int yy = now.y + dy[i];
            if(xx >= 1 && xx <= m && yy >=1 && yy <= n && !vis[xx][yy] && a[xx][yy] != '#' && disf[xx][yy] > now.step+1)         
   注意判断条件,如果不加disf[xx][yy] > now.step+1,
我就超时了,因为如果上一个火到某个点的速度比这个火到某点的快,
那么从这个点开始蔓延,肯定还是上一个火蔓延的快,就不用再加入队列白费力气再比较了。
            {
                vis[xx][yy] = 1;
                disf[xx][yy] = now.step+1;
                q.push((node)
                {
                    xx, yy, now.step+1
                });
            }
        }
    }
}
BFS算法(广度优先搜索算法)是一种用于图结构的搜索算法,它从问题的初始状态(起点)出发,通过遍历状态转换规则(图结构中的边),来寻找终结状态(终点)。BFS算法在解决一些问题时非常有用,比如求解迷宫最短路径、计算图像中的连通块等。下面是一个手写的BFS算法的实现模板: 1. 首先创建一个队列,用于存储待处理的节点。 2. 将起始节点加入队列,并标记为已访问。 3. 进入循环,直到队列为空。 a. 从队列中取出一个节点。 b. 遍历该节点的所有邻居节点。 - 如果邻居节点未被访问过,将其加入队列,并标记为已访问。 4. 返回结果。 根据上述模板,可以手写一个BFS算法的实现。需要注意的是,具体的实现可能因问题的不同而有所变化。在实际应用中,可以根据具体问题的要求,对算法进行适当的调整。 提供了BFS算法的基本原理和应用场景,可以作为参考。提供了标准C的图的实现和一些常用算法的实现,可以作为对BFS算法的实际运用的参考。同时,在实际编写代码时,可以使用手写哈希表来提高运行速度,如所述。 希望以上信息对你有所帮助,如果还有其他问题,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [标准C的图的实现+BFS和DFS遍历+Dijkstra算法+Prim算法+Kruskal算法实现](https://download.csdn.net/download/qq_35624030/12308217)[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: 33.333333333333336%"] - *2* [H 扫雷 / 手写哈希+bfs](https://blog.csdn.net/m0_74053777/article/details/129689287)[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: 33.333333333333336%"] - *3* [BFS算法简介](https://blog.csdn.net/sigd/article/details/123890402)[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: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值