搜索(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;
}
原文地址:https://www.cnblogs.com/scottding/p/3712838.html