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

转自
UVA11624 Fire! 两次BFS 读懂题意很重要

真是一道让我醉生梦死的题,调试了一天,最后吃饭的时候终于想出来哪里错了。。。

题意很简单,就是迷宫着火了,火势会蔓延,Joe要逃跑,看最后能不能逃出来,但没想到败在了英语上,大家注意,题意中用的是portions,这是一个复数形式,用词典一翻译,只看到了主体意思,没看到复数,结果就WA了20多次!!!

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

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

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

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

然后是我根据博主的代码写的,如有雷同,那也没办法(下面有博主的代码,写得很详细)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#define judge ne.x >= 0 && ne.x < n && ne.y >= 0 && ne.y < m
#define confirm vis[ne.x][ne.y] = 1
using namespace std;

struct str
{
    int x,y,step;
} fir,ne;

using namespace std;

int n,m,a,b;
int fire[1010][1010];
char s[1010][1010];
int nex[4] = {0,0,1,-1};
int ney[4] = {1,-1,0,0};
int vis[1010][1010];

queue<str> q;

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

void bfs_fire()
{
    memset(vis,0,sizeof(vis));
    while(q.size())
    {
        fir = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            ne.x = fir.x+nex[i];
            ne.y = fir.y+ney[i];
            ne.step = fir.step+1;
            if(vis[ne.x][ne.y] == 0 && (judge) && s[ne.x][ne.y] != '#')
            {
                fire[ne.x][ne.y] = ne.step;
                confirm;
                q.push(ne);
            }

        }
    }
}

void bfs_joe(int u,int v)
{
    memset(vis,0,sizeof(vis));
    clear_q();
    fir.x = u;
    fir.y = v;
    fir.step = 0;
    q.push(fir);
    while(q.size())
    {
        fir = q.front();
        if(fir.x == 0 || fir.x == n-1 || fir.y == 0 || fir.y == m-1)
        {
            printf("%d\n",fir.step+1);
            return ;
        }
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            ne.x = fir.x+nex[i];
            ne.y = fir.y+ney[i];
            ne.step = fir.step+1;
            if((judge) && fir.step+1 < fire[ne.x][ne.y] && vis[ne.x][ne.y] == 0 && s[ne.x][ne.y] == '.')
            {
                confirm;
                q.push(ne);
            }
        }
    }
    printf("IMPOSSIBLE\n");
}

int main()
{
    int t,a,b;
    scanf("%d",&t);
    while(t--)
    {
        memset(fire,0x3f,sizeof(fire));
        clear_q();
        scanf("%d %d",&n,&m);
        getchar();
        for(int i = 0; i < n; i++)
        {
            scanf("%s",s[i]);
            for(int j = 0; j < m; j++)
            {
                if(s[i][j] == 'J')
                {
                    a = i;
                    b = j;
                }
                else if(s[i][j] == 'F')
                {
                    fir.x = i;
                    fir.y = j;
                    fir.step = 0;
                    q.push(fir);
                }
            }
        }
        bfs_fire();
        bfs_joe(a,b);
    }
    return 0;
}

#include <iostream>
#include <queue>
#include <memory.h>
#include <stdio.h>
#define MAX 1100
using namespace std;
struct P//用来存储路径的节点
{
    int r,c;
    int step;
    P(int _r,int _c,int _s)
    {
        r=_r,c=_c,step=_s;
    }
    P(){}
};
 
const int dr[] = {0,0,1,-1};//构造函数
const int dc[] = {1,-1,0,0};
char Map[MAX][MAX];//存储地图
int visit[MAX][MAX];//BFS中最重要的visit数组
int times[MAX][MAX];//记录每个地方被烧到所需的时间
int r,c;//行,列
int Step;//记录最终逃脱所需的步数或时间
P J;
queue<P> q;//全局队列,这是本题的关键,也是一个大坑
 
void clear_q()//用来清除队列中的数据
{
    while(!q.empty())
        q.pop();
}
 
void show_time()//调试使用
{
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            cout<<times[i][j]<<' ';
        }
        cout<<endl;
    }
}
 
void bfs_fire()//预处理,求出每一个位置被烧到所需要花的时间
{
    int i;
    int R,C,Time;
    while(!q.empty())
    {
        P temp=q.front();
        q.pop();
        //cout<<temp.r<<' '<<temp.c<<endl;
        for(i=0;i<4;i++)
        {
            R=temp.r+dr[i];
            C=temp.c+dc[i];
            Time=temp.step+1;
            if(R>=0&&R<r&&C>=0&&C<c&&(Map[R][C]=='.'||Map[R][C]=='J')&&visit[R][C]==0)
            {
                //cout<<R<<' '<<C<<endl;
                visit[R][C]=1;
                q.push(P(R,C,Time));
                times[R][C]=Time;
            }
        }
    }
}
 
 
int bfs_joe()//逃跑路线,每走一个位置,不仅要看是否符合一般条件,还要用到上一步求出的时间
{
    int i;
    int R,C,Time;
    clear_q();
    q.push(J);
    visit[J.r][J.c]=1;
    while(!q.empty())
    {
        P temp=q.front();
        q.pop();
        //cout<<temp.r<<' '<<temp.c<<endl;
        if(temp.r==0||temp.r==(r-1)||temp.c==0||temp.c==(c-1))
        {
            Step=temp.step+1;
            return 1;
        }
        for(i=0;i<4;i++)
        {
            R=temp.r+dr[i];
            C=temp.c+dc[i];
            Time=temp.step+1;
            if(R>=0&&R<r&&C>=0&&C<c&&Map[R][C]=='.'&&visit[R][C]==0&&Time<times[R][C])
            {
                visit[R][C]=1;
                q.push(P(R,C,Time));
            }
        }
    }
    return 0;
}
 
int main()
{
    int i,j;
    int t;
    cin>>t;
    while(t--)
    {
        clear_q();//清空队列
        cin>>r>>c;
        for(i=0;i<r;i++)
        {
            cin>>Map[i];
            for(j=0;j<c;j++)
            {
                if(Map[i][j]=='F')//本题最最坑爹的地方,题意并没有直接对着火点的数量进行说明,但确实说了,隐含在portions 这个单词中,fuck,英文不好真捉急
                {
                    q.push(P(i,j,0));
                    times[i][j]=0;
                    visit[i][j]=1;
                }
                if(Map[i][j]=='J')
                {
                    J.r=i;
                    J.c=j;
                    J.step=0;
                }
            }
        }
 
        for(i=0;i<r;i++)//每个点的初始被烧所需的时间应该设为正无穷
        {
            for(j=0;j<c;j++)
            {
                times[i][j]=1000000007;
            }
        }
        memset(visit,0,sizeof(visit));//visit数组是两个BFS公用的,所以需要初始化一下
        bfs_fire();
        //show_time();
        memset(visit,0,sizeof(visit));
        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、付费专栏及课程。

余额充值