UVA11624 Fire! 两次BFS

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

注意起火点可以是多出
ac代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<ctype.h>
#include<stack>
#include<math.h>
#include <string>
#include<algorithm>
using namespace std;

typedef unsigned long long ULL;

struct p
{
    int r,c;
    int step;
    p(int tr,int tc,int tstep)
    {
        r=tr;
        c=tc;
        step=tstep;
    }
    p(){}
};

p J;

queue<p> q;

void clear_queue()
{
    while(!q.empty())
    {
        q.pop();
    }
}

const int maxn=1100;

char Map[maxn][maxn];
int book[maxn][maxn];
int times[maxn][maxn];
const int dr[] = {0,0,1,-1};//构造函数  
const int dc[] = {1,-1,0,0}; 

int r,c;
int step;

void bfs_fire()
{
    int time;
    while(!q.empty())
    {
        p t;
        t=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int tr=t.r+dr[i];
            int tc=t.c+dc[i];
            time=t.step+1;
            if(tr<0||tc<0||tr>r||tc>c)
                continue;
            if((Map[tr][tc]=='.'||Map[tr][tc]=='J')&&book[tr][tc]==0)
            {
                book[tr][tc]=1;
                q.push(p(tr,tc,time));
                times[tr][tc]=time;
            }
        }
    }
}

int bfs_joe()
{
    clear_queue();
    q.push(J);
    book[J.r][J.c]=1;
    while(!q.empty())
    {
        p t;
        t=q.front();
        q.pop();
        if(t.r==r-1||t.c==c-1||t.c==0||t.r==0)
        {
            step=t.step+1;
            return 1;
        }
        for(int i=0;i<4;i++)
        {
            int tr=t.r+dr[i];
            int tc=t.c+dc[i];
            int time=t.step+1;
            if(tr<0||tc<0||tr>r||tc>c)
                continue;
            if(Map[tr][tc]=='.'&&book[tr][tc]==0&&time<times[tr][tc])
            {
                q.push(p(tr,tc,time));
                book[tr][tc]=1;
            }
        }
    }
    return 0;
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);

    int n;
    cin>>n;
    while(n--)
    {
        clear_queue();
        cin>>r>>c;
        for(int i=0;i<r;i++)
        {
            cin>>Map[i];
            for(int j=0;j<c;j++)
            {
                if(Map[i][j]=='J')
                {
                    J.r=i;
                    J.c=j;
                    J.step=0;
                }
                if(Map[i][j]=='F')
                {
                    q.push(p(i,j,0));
                }
            }
        }
        for(int i=0;i<r;i++)
            for(int j=0;j<c;j++)
                times[i][j]=999999;
        memset(book,0,sizeof(book));
        bfs_fire();
        memset(book,0,sizeof(book));
        if(bfs_joe())
            cout<<step<<endl;
        else
            cout<<"IMPOSSIBLE"<<endl;
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值