UVA-----11624BFS

本文介绍了一个迷宫逃生问题的解决方案,使用BFS算法来确定人物能否在火灾蔓延到达之前逃出迷宫,并给出最快逃生时间。

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

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.
SampleInput
2
44
####
#JF#
#..#
#..#
33
###
#J.
#.F
SampleOutput
3
IMPOSSIBLE

先求火蔓延到各处的时间,再BFS求人逃跑的时间是否小于火蔓延的时间

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define INF 1e7+9
typedef long long ll;
const int N = 1010;
struct node{
    int x, y, t, id;
};
int n, m, ans, sum;
bool map[N][N];
char str[N];
queue<node>q;
int f[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
bool ok(int x ,int y){
   if(x >= 0 && x < n && y >= 0 && y < m && !map[x][y]){
        return true;
   }
   return false;
}
int bfs(){
    node nex;
    while(!q.empty()){
        node a = q.front();
        q.pop();
        for(int i = 0; i < 4; i++){
            nex.x = a.x + f[i][0];
            nex.y = a.y + f[i][1];
            nex.t= a.t + 1;
            nex.id = a.id;
            if(nex.id && (nex.x == -1 || nex.x == n || nex.y == -1 || nex.y == m)){
                return nex.t;
            }
            if(ok(nex.x, nex.y)){
                map[nex.x][nex.y] = 1;
                q.push(nex);
            }
        }
    }
    return -1;
}
int main(){
    int t;
    scanf("%d", &t);
    while(t--){
        node s1, s2;
        scanf("%d%d", &n, &m);
        while(!q.empty()){
            q.pop();
        }
        for(int i = 0; i < n; i++){
            scanf(" %s", str);
            for(int j = 0; j < m; j++){
                if(str[j] == '.'){
                    map[i][j] = 0;
                }
                else if(str[j] == '#'){
                    map[i][j] = 1;
                }
                else if(str[j] == 'F') {
                    map[i][j] = 1, s1.x = i, s1.y = j, s1.t = 0, s1.id = 0;
                    q.push(s1);
                }
                else if(str[j] == 'J'){
                    map[i][j] = 1, s2.x = i, s2.y = j, s2.t = 0, s2.id = 1;
                }
            }
        }
        q.push(s2);
        int ans = bfs();
        if(ans == -1){
            printf("IMPOSSIBLE\n");
        }
        else{
            printf("%d\n", ans);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值