uva 11624 FIRE!

Problem Description:

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 Specif**

> 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.

**Simple Output**

3
IMPOSSIBLE


**问题分析:**
简单图论问题~~先宽搜一回求出每个格子着火的时间
然后再宽搜一回求最短路。。。不过要注意细节哦!

#include<iostream>
#include<queue>
#include<cstring>
#define F(a,b) for(int a = 0 ; a < (b); a++ )
using namespace std;

typedef struct point p;
struct point
{
    int x;
    int y;
    int d;
    bool operator <(const point &a)const{return d<a.d;}
};

int m,n;
int line[4] = {0,0,1,-1};
int lie[4] = {1,-1,0,0};
int x0,y0;
int map[1010][1010];
int t[1010][1010];
queue<p>f;

bool inside(int x,int y)
{
    return x<m&&x>=0&&y<n&&y>=0;
}
void bfs1()
{
    while(!f.empty())
    {
        p& fro = f.front();
        f.pop();
        F(i,4)
        if(inside(fro.x+line[i],fro.y+lie[i])&&map[fro.x+line[i]][fro.y+lie[i]]==0&&t[fro.x+line[i]][fro.y+lie[i]]==-1)
        {
            t[fro.x+line[i]][fro.y+lie[i]] = t[fro.x][fro.y] + 1;
            f.push((p){fro.x+line[i],fro.y+lie[i],0});  
        }
    }
}
int bfs2()
{
    queue<p>q;
    q.push((p){x0,y0,0});
    while(!q.empty())
    {
        p& xx = q.front();
        q.pop();
        if(xx.x==0||xx.x==m-1||xx.y==0||xx.y==n-1)return xx.d+1;
        F(i,4)
        if(inside(xx.x+line[i],xx.y+lie[i])&&map[xx.x+line[i]][xx.y+lie[i]]==0&&xx.d+1<t[xx.x+line[i]][xx.y+lie[i]])
        {
            q.push((p){xx.x+line[i],xx.y+lie[i],xx.d+1});
        }
    }
    return 0;
}
int main()
{
    memset(t,-1,sizeof(t));
    int T;
    cin >> T;
    while(T--)
    {
        cin >> m >> n;
        F(i,m)F(j,n)
        {
            char ch;
            cin >> ch;
            if(ch == '#')map[i][j]=-1;
            if(ch == 'F'){map[i][j]=1;f.push((p){i,j,0});t[i][j] = 0;}
            if(ch == 'J'){map[i][j]==0;x0=i;y0=j;}
            if(ch == '.')map[i][j]==0;
        }
        bfs1();
        int ans = bfs2();
        if(ans)cout << ans;
        else cout << " IMPOSSIBLE" ;
        if(T!=0)cout << endl;

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值