UVA - 11624Fire!

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
注:
思路:先用广搜,算火可扩散到该地点的最短时间并记录下来;再用广搜,算人逃脱到地点的最短时间与火扩散的记录时间对比,小于火时间即可在该地点逃脱;

1.点火地点有多个
2.刚开始就可以逃脱
3.可能存在火势扩散不到的地方;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
int  n,m;
char arr[1010][1010];//存迷宫路线
int fire[1010][1010];//用于人逃脱时,已走过的路程标记
int door[1010][1010];//用所有火扩散过的路程标记
int md[1010][1010];//用于每次火扩散时,已扩散过的路程标记
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
struct node{
   int x;
   int y;
   int num;
};
int j[1010][1010];//记录火扩散到地点的最短时间
int BFS(int rx,int ry){
    node p,q;
    p.x=rx;
    p.y=ry;
    p.num=1;
    queue<node>que;
    que.push(p);
    //人一开始就处于迷宫边界
    if(p.x==0||p.y==0||p.x==(n-1)||p.y==(m-1))
        return p.num;
    while(que.size()){
        p=que.front();
        que.pop();
        for(int i=0;i<4;i++){
           q.x=p.x+dir[i][0];
            q.y=p.y+dir[i][1];
            if(q.x<0||q.y<0||q.x>=n||q.y>=m||arr[q.x][q.y]=='#'||fire[q.x][q.y]==1)
                continue;
            q.num=p.num+1;
            fire[q.x][q.y]=1;
            //人逃脱到地点的最短时间小于火扩散的记录时间,或火
            //不会蔓延到该地点,都可使人逃脱;
            if(q.num<j[q.x][q.y]||door[q.x][q.y]==0){
               que.push(q);
               //人处于迷宫边界,即可逃脱
               if(q.x==0||q.y==0||q.x==(n-1)||q.y==(m-1))
                return q.num;
            }

        }
    }
    return -1;
}
void FBFS(int fx,int fy){
    j[fx][fy]=1;
    door[fx][fy]=1;
    md[fx][fy]=1;
    node p,q;
    p.x=fx;
    p.y=fy;
    p.num=1;
    queue<node>que;
    que.push(p);
    while(que.size()){
        p=que.front();
        que.pop();
        for(int i=0;i<4;i++){
            q.x=p.x+dir[i][0];
            q.y=p.y+dir[i][1];
            if(q.x<0||q.y<0||q.x>=n||q.y>=m||arr[q.x][q.y]=='#'|| md[q.x][q.y]==1)
                continue;
            q.num=p.num+1;
            //此处保证可扩散地点所存的是最短时间
            if(j[q.x][q.y]!=0){
                    if(j[q.x][q.y]>q.num){
                        j[q.x][q.y]=q.num;
                        door[q.x][q.y]=1;
                        md[q.x][q.y]=1;
                        que.push(q);
                    }

            }
             else{
                 j[q.x][q.y]=q.num;
                door[q.x][q.y]=1;
                 md[q.x][q.y]=1;
                que.push(q);
             }

        }
    }
}
int main()
{
    int T,i,t,rx,ry;
    int fx[1010],fy[1010],nf;
    scanf("%d",&T);
    while(T--){
            nf=0;
        memset(fire,0,sizeof(fire));
         memset(door,0,sizeof(door));
          memset(j,0,sizeof(j));
        scanf("%d %d",&n,&m);
        for(i=0;i<n;i++)
            scanf("%s",arr[i]);
         for(i=0;i<n;i++){
            for(t=0;t<m;t++){
                if(arr[i][t]=='J')
                    rx=i,ry=t;
                //fx和fy数组用于处理点火地点不止一个
                 if(arr[i][t]=='F'){
                      fx[nf]=i,fy[nf]=t;
                      j[i][t]=1;
                      nf++;
                 }

            }
         }
         //计算火扩散的时间,最终要保证可扩散地点所存的是最短时间
         while(nf--){
             memset(md,0,sizeof(md));
             FBFS(fx[nf],fy[nf]);
         }
       //人逃脱
        int  ov=BFS(rx,ry);
        if(ov!=-1)
            printf("%d\n",ov);
        else
            printf("IMPOSSIBLE\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值