Uva 11624 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


  题目大意:一个人要逃出迷宫,同时可能有多个地方起火,火势向四周蔓延,火和人不能到达墙,人必须在火之前到达该格子,火和人的速度一样,求人逃出来的最短时间。

  基本思路是:初始化火能到达的每一个格子的最短时间,一次bfs即可,首先初始化到达每个格子的步数为-1(相当于标记访问),找到每个火源并全部入队,搜索中如果该格子不为-1,代表已经被访问,那么再访问的步数一定会超过曾经访问过的步数,于是跳过该格子。

  接下来bfs人逃亡即可。


#include<stdio.h>
#include<string.h>
int T,m,n,ans,ok,step[1005][1005],bu[1005][1005]; // step用来初始化火,bu用来记录人
char mat[1005][1005];
struct C
{
    int x,y;
}q[10000100];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
void fire()
{
    int fr=0,re=0;
    memset(step,-1,sizeof(step)); //相当于标记访问,同时又记录步数
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++) {
            if(mat[i][j]=='F') {
                q[re].x=i;
                q[re++].y=j;
                step[i][j]=0;
            }
        }
    while(fr<re) {
        int x=q[fr].x;
        int y=q[fr].y;
        for(int i=0;i<4;i++)
        {
            int nx=x+dx[i];
            int ny=y+dy[i];
            if(nx>=n || nx<0 || ny>=m || ny<0) continue;
            if(step[nx][ny]!=-1) continue;
            if(mat[nx][ny]=='#') continue;
            step[nx][ny]=step[x][y]+1;
            q[re].x=nx;
            q[re++].y=ny;
        }
        fr++;
    }
}
void bfs()
{
    memset(bu,-1,sizeof(bu));  //同理
    int fr=0,re=0;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            if(mat[i][j]=='J') {
                q[re].x=i;
                q[re++].y=j;
                bu[i][j]=0;
            }
    while(fr<re)
    {
        int nx=q[fr].x;
        int ny=q[fr].y;
        for(int i=0;i<4;i++)
        {
            int fx=nx+dx[i];
            int fy=ny+dy[i];
            if(nx==0 || nx==n-1 || ny==0 || ny==m-1)  {
                ok=1;
                ans=bu[nx][ny]+1;
                return ;
            }
            if(fx>=n || fx<0 || fy>=m || fy<0) continue;
            if(bu[fx][fy]!=-1) continue;
            if(mat[fx][fy]=='#') continue;
	    /* 须特别注意,火有可能被墙包围,使得火无法蔓延。下句的意思是,如果火蔓延到这个格子,并且火到这个格子的步数小于或等于人到这个格子的步数,那么跳过此格子。假使火根本没有蔓延过来,理所当然可以人走这个格子。 */
            if(step[fx][fy]!=-1 && bu[nx][ny]+1>=step[fx][fy]) continue; 
            q[re].x=fx;
            q[re++].y=fy;
            bu[fx][fy]=bu[nx][ny]+1;

        }
        fr++;
    }
}
int main()
{
    scanf("%d%*c",&T);
    while(T--) {
        scanf("%d %d%*c",&n,&m);
        for(int i=0;i<n;i++)
            gets(mat[i]);
        fire();
        ok=0;
        bfs();
        if(ok) printf("%d\n",ans);
        else printf("IMPOSSIBLE\n");
    }
    return 0;
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值