UVA 11624(两次bfs)

本文详细解析了UVA11624迷宫逃生问题的算法解决方案,通过两次广度优先搜索(BFS)分别计算火势蔓延时间和Joe逃生路径,最终确定Joe是否能安全逃离迷宫及所需时间。

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

UVA 11624(两次bfs)

UVA - 11624

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

题意:有多个起火点,每个起火点可以往他四周烧,Joe在起火区域内,问Joe能不能从火场逃生。
分析:两次bfs就可解决,第一个bfs计算烧到当前格需要多少时间,第二个bfs计算Joe能否逃生
PS:注意是有多个起火点。。。

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
const int inf = 0x3f3f3f3f;
struct node{
    int x, y, s;
};
int n, m,fx, fy, jx, jy, ans;
char s[N][N];
bool vis[N][N];
int e1[N][N], e2[N][N];
int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
void bfs1(){
    memset(vis, false, sizeof(vis));
    queue<node> Q;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(s[i][j] == 'F'){
                vis[i][j] = true;
                Q.push((node){i, j, 0});
            }
        }
    }
    while(!Q.empty()){
        int xx = Q.front().x;
        int yy = Q.front().y;
        int ss = Q.front().s;
        Q.pop();
        for(int k = 0; k <= 3; k++){
            int tx = xx + Next[k][0];
            int ty = yy + Next[k][1];
            if(tx < 0 || ty < 0 || tx >= n || ty >= m || vis[tx][ty])
                continue;
            if(s[tx][ty] == '.'){
                vis[tx][ty] = true;
                Q.push((node){tx, ty, ss + 1});
                e1[tx][ty] = ss + 1;
            }
        }
    }
}
inline int bfs2(int x, int y){
    memset(vis, false, sizeof(vis));
    vis[x][y] = true;

    e2[x][y] = 0;
    queue<node> Q;
    Q.push((node){x, y, 0});
    while(!Q.empty()){

        int xx = Q.front().x;
        int yy = Q.front().y;
        int ss = Q.front().s;
        Q.pop();
        if(xx == 0 || yy == 0 || xx == n - 1 || yy == m - 1){
            return ss + 1;
        }
        for(int k = 0; k <= 3; k++){
            int tx = xx + Next[k][0];
            int ty = yy + Next[k][1];
            if(tx < 0 || ty < 0 || tx >= n || ty >= m || vis[tx][ty])
                continue;
            if(s[tx][ty] == '.' && ss + 1 < e1[tx][ty]){
                vis[tx][ty] = true;
                Q.push((node){tx, ty, ss + 1});
                e2[tx][ty] = ss + 1;
            }
        }
    }
    return  -1;
}
int main(){
    #ifdef ONLINE_JUDGE
    #else
        freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int t;
    scanf("%d", &t);
    while(t--){
        memset(e1, inf, sizeof(e1));
        scanf("%d%d", &n, &m);
        for(int i = 0; i < n; i++){
            scanf("%s", s[i]);
        }
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(s[i][j] == 'J')
                    jx = i, jy = j;
            }
        }
        bfs1();
        int ans = bfs2(jx, jy);
        if(ans == -1)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n", ans);
    }
    return 0;
}

posted on 2018-12-12 20:21 坤sir 阅读(...) 评论(...) 编辑 收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kunsir_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值