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


老题新做,虽然绕过了大坑,就是那个多起点,但还是在BFS的时候栽了个小跟头,忘记加visit数组,其实,Fire的BFS确实可以省略visit,因为time可以充当这一角色,但是Joe的BFS不行,否则会造成无限循环,超时。虽然代码几乎一模一样,但还是想贴上来,见证我的成长吧。

-------------------------------------------------------------------------------------------------------------------------------------


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


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

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

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

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

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


#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;
}


#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
#define MAX 1010
using namespace std;

char Map[MAX][MAX];
int Time[MAX][MAX];
bool vis[MAX][MAX];
int r,c;
int dr[]={-1,1,0,0};
int dc[]={0,0,-1,1};

struct P
{
    int r,c,t;
    P(int r_,int c_,int t_):r(r_),c(c_),t(t_){}
};
queue<P> F;
queue<P> J;


/**************Debug*************/

void show_Time()
{
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
            printf("%d ",Time[i][j]);
        printf("\n");
    }
}


/*******************************/



void clear()
{
    memset(vis,false,sizeof(vis));
    while(!F.empty())
        F.pop();
    while(!J.empty())
        J.pop();
}

bool judge(int nr,int nc)
{
    return (nr>=0&&nr<r&&nc>=0&&nc<c);
}

bool ok(int nr,int nc)
{
    return (nr==0||nr==r-1||nc==0||nc==c-1);
}

void bfs_f()
{
    int nr,nc,nt;
    while(!F.empty())
    {
        P f=F.front();
        F.pop();
        for(int i=0;i<4;i++)
        {
            nr=f.r+dr[i];
            nc=f.c+dc[i];
            nt=f.t+1;
            if(judge(nr,nc))
            {
                if((Map[nr][nc]=='.'||Map[nr][nc]=='J')&&nt<Time[nr][nc])
                {
                    F.push(P(nr,nc,nt));
                    Time[nr][nc]=nt;
                }
            }
        }
    }
}

int bfs_j()
{
    int nr,nc,nt;
    while(!J.empty())
    {
        P j=J.front();
        J.pop();
        if(ok(j.r,j.c))
            return j.t+1;

        for(int i=0;i<4;i++)
        {
            nr=j.r+dr[i];
            nc=j.c+dc[i];
            nt=j.t+1;
            if(judge(nr,nc))
            {
                if(vis[nr][nc]==false&&Map[nr][nc]=='.'&&nt<Time[nr][nc])
                {
                    J.push(P(nr,nc,nt));
                    vis[nr][nc]=true;
                }
            }
        }
    }
    return 0;
}

int main()
{
    int T,t;
    scanf("%d",&T);
    while(T--)
    {
        clear();
        scanf("%d%d",&r,&c);

        for(int i=0;i<r;i++)
        {

            scanf("%s",Map[i]);
            for(int j=0;j<c;j++)
            {
                Time[i][j]=1000000007;
                if(Map[i][j]=='J')
                {
                    J.push(P(i,j,0));
                    vis[i][j]=true;
                }

                else if(Map[i][j]=='F')
                    F.push(P(i,j,0));
            }
        }
        bfs_f();
        //show_Time();
        if(t=bfs_j())
            printf("%d\n",t);
        else
            printf("IMPOSSIBLE\n");
    }
    return 0;
}









  • 12
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值