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
                });
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值