UVA 11624(图广搜)

题目链接:点击打开链接

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<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1001;
int sx,sy,m,n,dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool vis[N][N];//标记火
bool vis1[N][N];//标记人
char a[N][N];
struct node
{
    int x,y,time;
};
bool can(int x,int y)
{
    if(x<0||x>=m||y<0||y>=n)
        return false;
    return true;
}
queue<node>fire;
void bfs()//火扩散
{
    node now,net;
    int T=fire.front().time;
    while(!fire.empty())
    {
        now=fire.front();
        if(now.time>T)//判断,把这一分钟之内的所有位置都走完
            break;
        fire.pop();
        for(int i=0;i<4;i++)
        {
            net.x=now.x+dir[i][0];
            net.y=now.y+dir[i][1];
            if(can(net.x,net.y)&&a[net.x][net.y]!='#'&&!vis[net.x][net.y])
            {
                vis[net.x][net.y]=true;
                vis1[net.x][net.y]=true;//火走了,人就不能走了
                net.time=now.time+1;
                fire.push(net);
            }
        }
    }
}
int bfs1()//人走
{
    queue<node>q;
    while(!q.empty()) q.pop();
    node now,net;
    now.x=sx,now.y=sy,now.time=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.time>fire.front().time)//控制时间,把一分钟之内的所有情况走完,再让火进行蔓延
            bfs();
        if(vis1[now.x][now.y]) continue;//判断必不可少,判断一下火有没有蔓延到人所在的地方,如果到了,那么就不符合情况
        if(now.x==0||now.x==m-1||now.y==0||now.y==n-1)
            return now.time;
        vis1[now.x][now.y]=true;//标记访问不许放在这
        for(int i=0;i<4;i++)
        {
            net.x=now.x+dir[i][0];
            net.y=now.y+dir[i][1];
            if(can(net.x,net.y)&&!vis1[net.x][net.y]&&a[net.x][net.y]=='.')
            {
                net.time=now.time+1;
                q.push(net);
            }
        }
    }
    return -1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,false,sizeof(vis));
        memset(vis1,false,sizeof(vis1));
        while(!fire.empty()) fire.pop();
        scanf("%d%d",&m,&n);
        for(int i=0;i<m;i++)
        {
            scanf("%s",a[i]);
            for(int j=0;j<n;j++)
            {
                if(a[i][j]=='F')
                {
                    node t;
                    vis[i][j]=vis1[i][j]=true;
                    t.x=i,t.y=j,t.time=0;
                    fire.push(t);
                }
                if(a[i][j]=='J')
                    sx=i,sy=j;//这里不能用vis1数组标记访问
            }
        }
        int ans=bfs1();
        if(ans==-1)
            printf("IMPOSSIBLE\n");
        else printf("%d\n",ans+1);
    }
}
//2
//4 4
//####
//#JF#
//#..#
//#..#
//3 3
//###
//#J.
//#.F

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值