UVA11624 Fire!

Fire!

Joe works in a maze. Unfortunately,portions of the maze have caught on fire, and the owner of the maze neglectedto create a fire escape plan. Help Joe escape the maze.

Given Joe's location in the maze and which squares of themaze are on fire, you must determine whether Joe can exit the maze before thefire 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 fourdirections from each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the fire may enter asquare that is occupied by a wall.

Input Specification

The first line of input contains a single integer, thenumber of test cases to follow. The first line of each test case contains thetwo integers R and C, separated byspaces, with 1 <= R,C <= 1000. Thefollowing R lines of the test case each contain one row of themaze. Each of these lines contains exactly C characters, andeach of these characters is one of:

·  #, a wall

·  ., a passable square

·  J, Joe's initialposition in the maze, which is a passable square

·  F, a square that is onfire

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 cannotexit the maze before the fire reaches him, or an integer giving the earliesttime Joe can safely exit the maze, in minutes.

Output for SampleInput

3

IMPOSSIBLE



题意:J希望能走出这个迷宫,然后F表示火苗的位置,然后F每秒可以向四个方向扩展。

思路:把F扩展的格子标记上最少的秒数,利用bfs最短路的应用,标记出来。

然后bfs一次J看是否能走到边界处即可。别忘了加火苗限制条件,秒数要比标记的秒数小才可以。

#include <cstdio>
#include <queue>
#include <cstring>

using namespace std;
const int MAXN=1000+7;
const int inf=1e9;
int n ,m;
int sx,sy;
char tu[MAXN][MAXN];
bool vis[MAXN][MAXN];
int step[MAXN][MAXN];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

struct node
{
    int x,y,step;
    node(int a,int b,int c)
    {
        x=a;
        y=b;
        step=c;
    }
};


void  bfs(int sx,int sy)
{
    queue<node>q;
    memset(vis,0,sizeof(vis));
    vis[sx][sy]=1;
    q.push(node(sx,sy,0));
    while(!q.empty())
    {
        node u=q.front();
        q.pop();
        if(u.x == 0 || u.x == n-1 || u.y == 0 || u.y == m-1)
        {
            printf("%d\n",u.step+1);
            return ;
        }
        for(int i = 0;i < 4 ; ++i)
        {
            int nx=u.x+dx[i];
            int ny=u.y+dy[i];
            if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && tu[nx][ny]!='#' && u.step+1 < step[nx][ny])
            {
                vis[nx][ny]=1;
                q.push(node(nx,ny,u.step+1));
            }
        }
    }
    puts("IMPOSSIBLE");

}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        queue<node>q;
        for(int i = 0 ; i < n ; ++i )
            for(int j = 0 ;j < m ; ++j)
        {
            vis[i][j]=0;
            step[i][j]=inf;
        }
        for(int i = 0 ; i < n ; ++i)
        {
            scanf("%s",tu[i]);
            for(int j = 0 ; j < m ; ++j)
            {
                if(tu[i][j]=='F')
                {
                    q.push(node(i,j,0));
                    vis[i][j]=1;
                }
                else if(tu[i][j]=='J')
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        //bfs出火苗覆盖到每个空点的时间
        while(!q.empty())
        {
            node u=q.front();
            q.pop();
            step[u.x][u.y]=u.step;
            for(int i = 0;i < 4;++i)
            {
                int nx=u.x+dx[i];
                int ny=u.y+dy[i];
                if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny] && tu[nx][ny] != '#')
                {
                    vis[nx][ny] = 1;
                    q.push(node(nx ,ny ,u.step+1));
                }
            }
        }
        //
        bfs(sx,sy);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值