GZHU18级寒假训练:Leo's Trial-G

UVA-11624

  • 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


这是一个双重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;
}

代码来源:https://blog.csdn.net/wr132/article/details/45399337

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值