UVA 11624 Fire!

本文介绍了一个迷宫逃生问题的解决方案,通过使用广度优先搜索(BFS)算法来帮助角色Joe在迷宫中躲避火焰并找到最快的逃生路径。文章详细解释了如何分别对火势蔓延和角色移动进行模拟,并最终判断是否能成功逃离。

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

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 Specification

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.

Sample Input

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

Output Specification

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.

Output for Sample Input

3
IMPOSSIBLE
 


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=1005;
const int INF=0x3f3f3f3f;
int n,m;
int sx,sy;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int visp[MAXN][MAXN];
int T[MAXN][MAXN];
struct node
{
    int x,y,s;
};
queue<struct node>q;
void BFS_Fire()
{
    int i;
    while(!q.empty())
    {
        struct node t;
        t=q.front();
        q.pop();
        for(i=0;i<=3;i++)
        {
            int xx=t.x+dir[i][0];
            int yy=t.y+dir[i][1];
            if(xx>=0&&xx<=n-1&&yy>=0&&yy<=m-1&&!vis[xx][yy]&&map[xx][yy]!='#')
            {
                vis[xx][yy]=1;
                struct node f;
                f.x=xx;
                f.y=yy;
                f.s=t.s+1;
                T[xx][yy]=f.s;
                q.push(f);
            }
        }
    }
}
int BFS_Man()
{
    queue<struct node>qq;
    while(!q.empty()) q.pop();
    struct node s;
    s.x=sx;
    s.y=sy;
    s.s=0;
    qq.push(s);
    visp[sx][sy]=1;
    while(!qq.empty())
    {
        int i;
        struct node t;
        t=qq.front();
        qq.pop();
        if(t.x==0||t.x==n-1||t.y==0||t.y==m-1) return t.s+1;
        for(i=0;i<=3;i++)
        {
            int xx=t.x+dir[i][0];
            int yy=t.y+dir[i][1];
            //if(xx==0||xx==n-1||yy==0||yy==m-1) return t.s+1;
            if(t.s+1>=T[xx][yy]) continue;
            if(xx>=0&&xx<=n-1&&yy>=0&&yy<=m-1&&!visp[xx][yy]&&map[xx][yy]!='#')
            {
                visp[xx][yy]=1;
                struct node f;
                f.x=xx;
                f.y=yy;
                f.s=t.s+1;
                qq.push(f);
            }
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        while(!q.empty()) q.pop();
        memset(vis,0,sizeof(vis));
        memset(visp,0,sizeof(visp));
        memset(T,INF,sizeof(T));
        int i,j;
        scanf("%d%d",&n,&m);
        for(i=0;i<=n-1;i++)
        {
            scanf("%s",map[i]);
        }
        for(i=0;i<=n-1;i++)
        {
            for(j=0;j<=m-1;j++)
            {
                if(map[i][j]=='J')
                {
                    sx=i;
                    sy=j;
                }
                if(map[i][j]=='F')
                {
                    struct node t;
                    t.x=i;
                    t.y=j;
                    t.s=0;
                    vis[i][j]=1;
                    T[i][j]=0;
                    q.push(t);
                }
            }
        }
        BFS_Fire();
        int ans=BFS_Man();
        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、付费专栏及课程。

余额充值