zoukankan      html  css  js  c++  java
  • 搜索(BFS)

    Problem B: 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 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 <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <string>
    #include <queue>
    #include <vector>
    
    using namespace std;
    
    #define INF 1<<29
    #define eps 1e-8
    #define A system("pause")
    #define rep(i,h,n) for(int i=(h);i<=(n);i++)
    #define ms(a,b) memset((a),(b),sizeof(a))
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    
    const int maxn=2000+5;
    int dx[]={0,1,-1,0};
    int dy[]={1,0,0,-1};
    struct dot
    {
        int x,y;
        bool isfire;
        int time;
    };
    
    int vis[maxn][maxn];
    char map[maxn][maxn];
    int test,n,m;
    
    inline bool in(dot sgx)
    {
         if(sgx.x>=0&&sgx.x<n&&sgx.y>=0&&sgx.y<m) return true;
         return false;
    }
    
    int main()
    {
        scanf("%d",&test);
        while(test--)
        {
             dot gx;
             scanf("%d%d",&n,&m);
             rep(i,0,n-1) scanf("%s",map[i]);
             queue<dot> q;
             while(!q.empty()) q.pop();
             ms(vis,0);
             rep(i,0,n-1) rep(j,0,m-1)
             {dot F;
                  if(map[i][j]=='J') gx.x=i,gx.y=j,gx.isfire=0,gx.time=0;
                  else if(map[i][j]=='F')   vis[i][j]=1,F.x=i,F.y=j,F.isfire=1,F.time=0,q.push(F);
                  else if(map[i][j]=='#')   vis[i][j]=1;
             }
             q.push(gx);
             int ret=0;
             while(!q.empty())
             {
                 dot next;
                 gx=q.front(),q.pop();
                 rep(i,0,3)
                 {
                     next.x=gx.x+dx[i];
                     next.y=gx.y+dy[i];
                     next.isfire=gx.isfire;//BFS的时候注意"火势蔓延",相邻格子火势是相通的;
                     next.time=gx.time+1;
                     if(in(next))
                     {
                         if(!vis[next.x][next.y]) vis[next.x][next.y]=1,q.push(next);
                     }
                     else if(next.isfire==0)
                     {
                         ret=next.time;//记录当前答案;
                         break;
                     }
                 }
                 if(ret>0)break;
             }
             if(!ret) std::puts("IMPOSSIBLE");
             else printf("%d
    ",ret);
        }
        //A;
        return 0;
    }
    
  • 相关阅读:
    cocospods 卡在 Analyzing dependencies
    android px、sp、dp之间的互转
    Android 4.4环境搭建——Android SDK下载与安装
    我心中的核心组件(可插拔的AOP)~大话开篇及目录
    EF架构~AutoMapper对象映射工具简化了实体赋值的过程
    我心中的核心组件(可插拔的AOP)~第二回 缓存拦截器
    EF架构~为EF DbContext生成的实体添加注释(T5模板应用)
    品味编程~底层开发人员应该这样设计一个字体类
    Study notes for Clustering and K-means
    深入理解Oracle索引(25):一招鲜、吃遍天之单字段索引创建思路
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4237420.html
Copyright © 2011-2022 走看看