搜索练习9 https://cn.vjudge.net/contest/81658#problem/J Fire! bfs

本文介绍了一个迷宫逃生问题的解决方案,使用广度优先搜索(BFS)算法来确定人物能否在火灾蔓延到其位置之前逃离迷宫。文章详细解释了算法实现思路及代码细节。

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


https://cn.vjudge.net/contest/81658#problem/J

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

题意:现在有个人在迷宫里,现在迷宫有一些地方着火了,他想逃出来,他和火的速度都是一秒一个格子,他先走然后火再走。输出他最快多少时间能逃出来,若不能则输出IMPOSSIBLE;

思路:bfs,从队列里拿出一个点,首先要判断他是火,还是人,走过的点标记成不一样的。一定要先把人放入队列,然后才能放火。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<string>
#define LL long long
#define eps 1e-8
using namespace std;
const int mod = 1e7+7;
const int INF = 1e8;
const int inf = 0x3f3f3f3f;
const int maxx = 100100;
const int N = 1050;
int n,m;
char ma[N][N];
struct node
{
    int x,y,step;
} star;
int dx[5]= {0,0,1,-1};
int dy[5]= {1,-1,0,0};
int v[N][N];
queue<node>q;
int bfs()
{
    node p1,p2;
    while(!q.empty())
    {
        p1=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            p2=p1;
            if(v[p2.x][p2.y]==2&&p2.step)continue;//如果人走过的这个地方被火走到了,人就不能从这再走了。例如样例2
            p2.x+=dx[i];
            p2.y+=dy[i];
            if(p2.step)
            {
                if(p2.x<0||p2.y<0||p2.x>=n||p2.y>=m)
                    return p2.step;
                if(ma[p2.x][p2.y]=='.'&&!v[p2.x][p2.y])//人只能走标记是0的点
                {
                    p2.step++;
                    v[p2.x][p2.y]=1;
                    q.push(p2);
                }
            }
            else
            {
                if(p2.x<0||p2.y<0||p2.x>=n||p2.y>=m)continue;
                if(ma[p2.x][p2.y]!='#'&&v[p2.x][p2.y]<2)//火可以走标记为0和1的地方
                {
                    v[p2.x][p2.y]=2;
                    q.push(p2);
                }
            }
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        while(!q.empty())
            q.pop();
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++)
        {
            scanf("%s",ma[i]);
            for(int j=0; j<m; j++)
                if(ma[i][j]=='J')
                {
                    star.x=i,star.y=j;
                    star.step=1;//步数为正表示是人。
                    v[i][j]=1;//人标记为1
                    q.push(star);
                }
        }
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                if(ma[i][j]=='F')
                {
                    star.x=i,star.y=j;
                    star.step=0;//步数为0表示是火。
                    v[i][j]=2;//火标记为2
                    q.push(star);
                }
        memset(v,0,sizeof(v));
        int ans= bfs();
        if(ans==-1)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n",ans);
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值