zoukankan      html  css  js  c++  java
  • 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
    ");
            else printf("%d
    ",ou);
        }
        return 0;
    }
  • 相关阅读:
    Feign Ribbon Hystrix 关系剖析
    Activiti 分布式方案实现探讨
    Flink任务架构分析
    Activiti 数据库表梳理
    负载均衡方案优缺点探讨
    公文流转系统
    css美化界面
    动手动脑(二)
    csslayui树练习
    css点名
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10139285.html
Copyright © 2011-2022 走看看