kuangbin fire搜索bfs

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

 

 

要分成两条路,一条是火烧得,另一条是人走的,把火烧到每一个地方的时间记录下来

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
struct node
{
    int x,y;
    int step;
    node(int x=0,int y=0) : x(x),y(y) {} //构造函数
};
const int inf=0x3f3f3f;
int to[4][2]={0,1,1,0,0,-1,-1,0},t[1005][1005],n,m,sx,sy,ou;
bool vit[1005][1005];
char a[1005][1005];
void bfs1()
{
    queue<node>que;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            if(a[i][j]=='F')
            {
                t[i][j]=0;
                que.push(node(i,j));
            }
        }
    }
    while(!que.empty())
    {
        node exa=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int tx=exa.x+to[i][0];
            int ty=exa.y+to[i][1];

            if(a[tx][ty]=='#') continue;
            if(tx<1||tx>n||ty<1||ty>m) continue;
            if(t[exa.x][exa.y]+1>=t[tx][ty]) continue;//不用vis,因为时间短的优先考虑,重点!!!

            t[tx][ty]=t[exa.x][exa.y]+1;//记录时间
            que.push(node(tx,ty));
        }
    }
}

void bfs2()
{
    node ee;
    queue<node>que;
    ee.x=sx;
    ee.y=sy;
    ee.step=0;
    que.push(ee);
    vit[ee.x][ee.y]=1;
    while(!que.empty())
    {
        ee=que.front();
        node zz;
        que.pop();
        if(ee.x==1||ee.y==1||ee.x==n||ee.y==m)
        {
            ou=ee.step+1;
            return ;
        }
        for(int i=0;i<4;i++)
        {

            zz.x=ee.x+to[i][0];
            zz.y=ee.y+to[i][1];
            zz.step=ee.step+1; //step记录每一个位置所用的时间与火的时间作比较

            if(zz.x<1||zz.y<1||zz.x>n||zz.y>m) continue;
            if(a[zz.x][zz.y]=='#') continue;
            if(vit[zz.x][zz.y]) continue;
            if(zz.step>=t[zz.x][zz.y]) continue;

            vit[zz.x][zz.y]=1;
            que.push(zz);
        }
    }
}

int main()
{
    int c;
    cin>>c;
    while(c--)
    {
        memset(vit,0,sizeof(vit));
        memset(t,inf,sizeof(t));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",a[i]+1);
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]=='J'){sx=i;sy=j;}
            }
        }
        ou=inf;
        bfs1();
        bfs2();
        if(ou>=inf) printf("IMPOSSIBLE\n");
        else printf("%d\n",ou);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/EchoZQN/p/10139285.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值