[bfs] fzu oj 2196 Escape

题意:

一个迷宫里面从开始走到终点的最短步数。

但是这个迷宫里面有许多的火山,会喷岩浆,岩浆每秒向四周蔓延,岩浆到过的点不能走,但是人的移动优先于岩浆。

意思就是,如果某一个时刻,岩浆和人同时到达,那么如果这个点是出口的话,这个点是可以走的。

思路:

首先bfs预处理所有的点火山蔓延到的最小时间,就是将所有火山压进队做bfs。

接着用人做一遍bfs,注意考虑上面的那个人的移动优先于岩浆。

代码:

#include"cstdlib"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"queue"
#include"algorithm"
#include"iostream"
#include"map"
using namespace std;
#define eps 1e-13
#define ll __int64
int time[1234][1234];
int used[1234][1234];
int n,m;
char mp[1234][1234];
int dis[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int x,y,t;
};
void bfs1()
{
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) time[i][j]=-1;
    node cur,next;
    queue<node>q;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(mp[i][j]=='!')
            {
                cur.x=i;
                cur.y=j;
                cur.t=0;
                time[i][j]=0;
                q.push(cur);
            }
        }
    }
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            next.x=cur.x+dis[i][0];
            next.y=cur.y+dis[i][1];
            next.t=cur.t+1;
            if(next.x<0 || next.y<0 || next.x>=n || next.y>=m || mp[next.x][next.y]=='#' || time[next.x][next.y]!=-1) continue;
            time[next.x][next.y]=next.t;
            q.push(next);
        }
    }
    return ;
}
int bfs2()
{
    for(int i=0; i<n; i++) for(int j=0; j<m; j++) used[i][j]=0;
    node cur,next;
    queue<node>q;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(mp[i][j]=='S')
            {
                cur.x=i;
                cur.y=j;
                cur.t=0;
                used[i][j]=1;
                q.push(cur);
            }
        }
    }
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        for(int i=0; i<4; i++)
        {
            next.x=cur.x+dis[i][0];
            next.y=cur.y+dis[i][1];
            next.t=cur.t+1;
            if(next.x<0 || next.y<0 || next.x>=n || next.y>=m || mp[next.x][next.y]=='#' || used[next.x][next.y] || time[next.x][next.y]<next.t) continue;
            used[next.x][next.y]=1;
            if(mp[next.x][next.y]=='E') return 1;
            else if(next.t!=time[next.x][next.y]) q.push(next);
        }
    }
    return 0;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++) scanf("%s",mp[i]);
        bfs1();
        int ans=bfs2();
        puts(ans?"Yes":"No");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值