H - Fire! (bfs的应用)

该博客讨论了一个迷宫逃生问题,其中主角Joe需要在火势蔓延前找到出口。利用BFS算法,同时模拟Joe和火的移动,判断Joe是否能安全逃脱,并给出最短逃生时间。输入包含迷宫大小及布局,输出为是否可能逃脱或最早的逃脱时间。博客通过样例解释了算法的实现细节和优化策略。
摘要由CSDN通过智能技术生成

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

 

题目大意:在一个图里, 有一个人和多团火,人和火同时移动, 看人是否可以顺利逃生

解析:解法要用两个bfs,在人寻找逃生路线时每到一个新的位置就要比较人到这个位置的步数和火到这个位置的步数,来判断 断是否是安全位置, 首先我们不能每到一个位置再去bfs火到这个位置的步数,这样是会超时的,原因不解释,其次,一定要记住火有很多团, 在火的bfs里找到火的位置都到压进队列里, 最后在一个点火到达的时间是要取最小值的,火第一次到达这个位置的时间一定是最小的,原因不解释, 所以这个题大概就是这个意思了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
const double pi = acos(-1.0);
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + 5;

int T, r, c, ans, x1, y11;
char mp[1007][1007];
int vis[1007][1007];
int t[1007][1007];

struct node
{
    int x, y;
    int step;
} nw, nxt;

int Next[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};


void bfs1()
{
    queue<node> q;
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            if(mp[i][j] == 'F')
            {
                nw.x = i;
                nw.y = j;
                t[i][j] = 0;
                q.push(nw);
            }
        }
    }
    q.push(nw);
    while(!q.empty())
    {
        nw = q.front(), q.pop();
        for(int i = 0; i < 4; i++)
        {
            nxt.x = nw.x + Next[i][0];
            nxt.y = nw.y + Next[i][1];
            if(nxt.x >= 0 && nxt.x < r && nxt.y >= 0 && nxt.y < c && mp[nxt.x][nxt.y] != '#' && t[nxt.x][nxt.y] > t[nw.x][nw.y] + 1)
            {
                t[nxt.x][nxt.y] = t[nw.x][nw.y] + 1;
                q.push(nxt);
            }
        }
    }
}

void bfs2()
{
    nw.x = x1;
    nw.y = y11;
    nw.step = 0;
    vis[x1][y11] = 1;
    queue<node> q;
    q.push(nw);
    while(!q.empty())
    {
        nw = q.front();
        q.pop();
        if(nw.x ==0 || nw.x == r - 1 || nw.y == 0 || nw.y == c - 1)
        {
            ans = nw.step + 1;
            return;
        }
        for(int i = 0; i < 4; i++)
        {
            nxt.x = nw.x + Next[i][0];
            nxt.y = nw.y + Next[i][1];
            if(nxt.x >= 0 && nxt.x < r && nxt.y >= 0 && nxt.y <c && mp[nxt.x][nxt.y] != '#' && nw.step + 1 < t[nxt.x][nxt.y] && vis[nxt.x][nxt.y] == 0)
            {
                vis[nxt.x][nxt.y] = 1;
                nxt.step = nw.step + 1;
                q.push(nxt);
            }
        }
    }
}

int main()
{
    cin >> T;
    while(T--)
    {
        cin >> r >> c;
        for(int i = 0; i < r; i++)
        {
            cin >> mp[i];
            for(int j = 0; j < c; j++)
            {
                if(mp[i][j] == 'J')
                {
                    x1= i;
                    y11 = j;
                }
            }
        }
        memset(vis, 0, sizeof(vis));
        memset(t, inf, sizeof(t));
        ans = inf;
        bfs1();
        bfs2();
        if(ans >= inf) cout << "IMPOSSIBLE" << endl;
        else cout << ans << endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值