UVA - 11624 Fire! (BFS) (day_5_G)

面对迷宫大火,乔必须迅速找到安全出口。此题探讨了在火势蔓延的情况下,使用BFS算法判断乔是否能安全逃离迷宫,并计算最快逃生时间。

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

Fire!

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

题目简述:

J困在迷宫里,迷宫里可能有火,火每秒向上下左右蔓延一格,但不能烧到墙上,问小J能不能成功跑出来。

题目分析:

处理一下每个格子着火的时间,初始化为无穷,着火后不能踩上去。BFS即可。

代码实现:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
//#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
#define pf          printf
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define ms(i,j) memset(i,j,sizeof(i))

int n,m,fire[1005][1005],d[4][2]= {-1,0,1,0,0,-1,0,1};
char a[1005][1005];
bool mark[1005][1005];
struct sb
{
    int x,y,s;
} tmp,now,bgn;
bool valid()
{
    return tmp.x>=0&&tmp.x<n&&tmp.y>=0&&tmp.y<m;
}
bool ok()
{
    if(now.x==0||now.y==0||now.x==n-1||now.y==m-1)
        return 1;
    else
        return 0;
}
void bfs()
{
    queue<sb> line;
    ms(mark,0);
    line.push(bgn);
    mark[bgn.x][bgn.y]=1;
    while(!line.empty())
    {
        now=line.front();
        line.pop();
        if(ok())
        {
            pf("%d\n",now.s+1);
            return;
        }
        for(int i=0; i<4; i++)
        {
            tmp.x=now.x+d[i][0];
            tmp.y=now.y+d[i][1];
            tmp.s=now.s+1;
            if(valid()&&!mark[tmp.x][tmp.y]&&tmp.s<fire[tmp.x][tmp.y]&&a[tmp.x][tmp.y]!='#')
            {
                mark[tmp.x][tmp.y]=1;
                line.push(tmp);
            }
        }
    }
    pf("IMPOSSIBLE\n");
}
int main()
{
    int T;
    queue<sb> q;
    scanf("%d",&T);
    while(T--)
    {
        ms(mark,0);
        fill(fire[0],fire[0]+1005*1005,INF);
        ms(a,0);
        sff(n,m);
        for(int i=0; i<n; i++)
        {
            scanf("%s",a[i]);
            for(int j=0; j<m; j++)
            {
                if(a[i][j]=='J')
                    bgn= {i,j,0};
                if(a[i][j]=='F')
                {
                    q.push(tmp= {i,j,0});
                    mark[i][j]=1;
                    fire[i][j]=0;
                }
            }
        }
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            for(int i=0; i<4; i++)
            {
                tmp.x=now.x+d[i][0];
                tmp.y=now.y+d[i][1];
                tmp.s=now.s+1;
                if(valid()&&!mark[tmp.x][tmp.y]&&a[tmp.x][tmp.y]!='#')
                {
                    mark[tmp.x][tmp.y]=1;
                    fire[tmp.x][tmp.y]=tmp.s;
                    q.push(tmp);
                }
            }
        }
        bfs();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值