----- 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
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
题目大意:
Joe被困在一个着火的迷宫里,火源可以向上下左右四个方向同时蔓延(不一定只有一个火源),Joe可以逃向上下左右的某一个方向,火移动一个方格花一分钟,人每移动一个方格花一分钟,问你Joe是否可以逃出迷宫(任何一个边界都可以看成出口),若可以则输出所花费的最小时间,若不行则输出”IMPOSSIBLE”.

解题思路:
两次bfs,bfs1用来计算火蔓延到某个方格时的时间(time1[i][j]代表火蔓延到i,j位置时候的时间),bfs2则用来计算人到达某个地方时的时间(time2[i][j]代表人逃到i,j位置时候的时间),人能不能逃到i,j方格(1.若该方格火势不会蔓延到(time1[i][j]==-1);2.火势会蔓延到该方格,但是人要比火先到达,即(time2[i][j]>time[i][j];)

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;

int n,m;
char map[1010][1010];
int time1[1010][1010],time2[1010][1010];///time1[i]代表火势蔓延到i点的时候的时间,time2[i]代表人逃到i点的时候的时间
int dri[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
queue<pair<int,int> >que;

void bfs1()
{
    memset(time1,-1,sizeof(time1));
    while(!que.empty()) que.pop();
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            if(map[i][j]=='F')
            {
                time1[i][j]=0;///火源位置处的时间都为0
                que.push(make_pair(i,j));///将火源的坐标加入队列
            }
    while(!que.empty())///队列非空的时候
    {
        pair<int,int> tmp=que.front();///取出队首元素
        que.pop();
        int x=tmp.first;///将x赋值为tmp中的第一个元素
        int y=tmp.second;///将y赋值为tmp中的第二个元素
        for(int k=0; k<4; k++) ///四个方向
        {
            int dx=x+dri[k][0];
            int dy=y+dri[k][1];
            if(dx<0||dy<0||dx>=n||dy>=m) continue;///出界
            if(map[dx][dy]=='#'||time1[dx][dy]!=-1) continue;///墙或者是该点之前火势已经蔓延过了
            que.push(make_pair(dx,dy));///将新的点的坐标加入队列中
            time1[dx][dy]=time1[x][y]+1;///到达该点的时间为上一步加一
        }
    }
}

int bfs2()
{
    memset(time2,-1,sizeof(time2));
    while(!que.empty()) que.pop();
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            if(map[i][j]=='J')
            {
                time2[i][j]=0;///Joe的起始位置的时间为0
                que.push(make_pair(i,j));///将Joe的起始坐标加入队列
            }
    while(!que.empty())
    {
        pair<int,int> tmp=que.front();///取出队首元素
        que.pop();
        int x=tmp.first;
        int y=tmp.second;
        if(x==0||y==0||x==n-1||y==m-1) return time2[x][y]+1;///已经到达的四个边界中的某一个
        for(int k=0; k<4; k++)
        {
            int dx=x+dri[k][0];
            int dy=y+dri[k][1];
            if(dx<0||dy<0||dx>=n||dy>=m) continue;///出界
            if(map[dx][dy]=='#'|| time2[dx][dy]!=-1) continue;///墙或者已经被走过了
            if(time1[dx][dy]!=-1&&time2[x][y]+1>=time1[dx][dy]) continue;///这一点火势一定会蔓延到,并且人比火势后到达,或者两者同时到达
            que.push(make_pair(dx,dy));
            time2[dx][dy]=time2[x][y]+1;
        }
    }
    return -1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
            for(int i=0; i<n; i++)
                scanf("%s",map[i]);
            bfs1();
            int ans=bfs2();
            if(ans==-1) printf("IMPOSSIBLE\n");
            else printf("%d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值