搜索(BFS)

本文介绍了一个迷宫逃脱问题的算法解决方案,重点在于如何利用BFS(宽度优先搜索)算法帮助迷宫中的角色Joe在火灾中找到最快的安全出口。文章详细解释了输入规格、输出规格以及样例输入输出,并提供了完整的C++代码实现。

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\n",ret);
    }
    //A;
    return 0;
}

 

转载于:https://www.cnblogs.com/NYNU-ACM/p/4237420.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值