J - Fire! UVA - 11624 (多起点BFS+两次BFS)

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要逃跑,看最后能不能逃出来,但没想到败在了英语上,大家注意,题意中用的是portions,这是一个复数形式,用词典一翻译,只看到了主体意思,没看到复数,结果就WA了好多次!!!

也就是说,Joe的起点唯一,但是起火的地方并不是唯一的,这是我们首先要明确的。

关键是之后应该怎样处理火势的蔓延与Joe逃跑这两个过程的关系,我们要清楚:只有火势可以影响Joe的逃跑路线,但Joe的逃跑路线绝对不能影响火势,所以这两个过程不可能出现在同一个BFS中。

可以这样想:既然火势的蔓延时不随人的主观意愿而改变的,那么我们可以先让火势肆意蔓延,看它到底能烧到哪里,以及烧到某个地方所需要的时间,这样,主人公在逃跑的过程中,只要在火势到达之前赶到某个地方就可以了。

综上,需要两个BFS,第一个计算火势蔓延到任意一点所需要的时间,如果火势永远到达不了某些点,就把这些点的时间设为正无穷,之后再搜索Joe的逃跑路线,条件要增加时间这一项,只要Joe能到达迷宫的边界,就算逃出来了。

代码:


//Full of love and hope for life

#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
#include <queue>

using namespace std;

struct node
{
    int x,y,step;
};

int flag[1010][1010];
int ft[1010][1010];
int b,c;
int nex[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
char s[1010][1010];
int nn[1010],mm[1010];
int p;

void bfsfire()//存火的运动坐标与时间
{
    node n,m;
    queue<node>q;
    for(int j=1; j<=p; j++)//直接全部存入所有火的坐标,不然会t
    {
        n.x=nn[j];
        n.y=mm[j];
        n.step=0;
        flag[n.x][n.y]=1;
        ft[n.x][n.y]=0;
        q.push(n);
    }
    while(!q.empty())
    {
        m=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            n.x=m.x+nex[i][0];
            n.y=m.y+nex[i][1];
            n.step=m.step+1;
            if(s[n.x][n.y]=='#'||flag[n.x][n.y]==1||n.x<1||n.x>b||n.y<1||n.y>c)
            {
                continue;
            }
            flag[n.x][n.y]=1;
            if(ft[n.x][n.y]>n.step)
            {
                ft[n.x][n.y]=n.step;
            }
            q.push(n);
        }
    }
}

void bfs(int x,int y)
{
    node n,m;
    queue<node>q;
    n.x=x;
    n.y=y;
    flag[n.x][n.y]=1;
    n.step=0;
    q.push(n);
    while(!q.empty())
    {
        m=q.front();
        q.pop();
        if(m.x==1||m.x==b||m.y==1||m.y==c)//到达边界要加1
        {
            cout << m.step+1 << endl;;
            return ;
        }
        for(int i=0; i<4; i++)
        {
            n.x=m.x+nex[i][0];
            n.y=m.y+nex[i][1];
            n.step=m.step+1;
            if(s[n.x][n.y]=='#'||flag[n.x][n.y]==1||n.x<1||n.x>b||n.y<1||n.y>c||n.step>=ft[n.x][n.y])//条件
            {
                continue;
            }
            flag[n.x][n.y]=1;
            q.push(n);
        }
    }
    cout << "IMPOSSIBLE" << endl;
}

int main()
{
    int a;
    cin >> a;
    while(a--)
    {
        p=0;
        int g,h;
        memset(ft,1000000,sizeof(ft));
        cin >> b >> c;
        for(int i=1; i<=b; i++)
        {
            for(int j=1; j<=c; j++)
            {
                cin >> s[i][j];
                if(s[i][j]=='J')//找J
                {
                    g=i;
                    h=j;
                }
                if(s[i][j]=='F')//找F
                {
                    p++;
                    nn[p]=i;
                    mm[p]=j;
                }
            }
        }
        memset(flag,0,sizeof(flag));//初始化
        bfsfire();
        memset(flag,0,sizeof(flag));//初始化
        bfs(g,h);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值