UVA 11624(BFS+火追人)

题意:乔在迷宫中工作。不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划。请帮助乔逃离迷宫。根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必须确定火焰烧到他之前,乔是否可以离开迷宫,如果能离开他能跑多快。 乔和火每分钟移动一个方格,上、下、左、右,四个方向中的一个。火势向四个方向同时蔓延。乔可以从迷宫的任何一个边界逃离迷宫。无论是乔还是火都不会到达有墙的位置。

思路:

这题可以看出是人追火,即火先走,火走过的地方人肯定就不能走了。所以先把火全部push进去,再push人,再BFS就OK了。

注意:

因为我没注意到火不是唯一的,所以WA了很多次。。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=200005;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int n,m;
char g[1005][1005];
struct node
{
    int x,y,step,fire;
};
node s,f;
int dx[]= {0,0,-1,1};
int dy[]= {1,-1,0,0};
int vis[1005][1005];
node huo[1005];
int tot=0;
void bfs()
{
    queue<node> q;
    memset(vis,0,sizeof(vis));
    for(int i=0; i<tot; i++)
    {
        q.push(huo[i]);
        vis[huo[i].x][huo[i].y]=1;
    }
    q.push(s);
    vis[s.x][s.y]=1;
    while(!q.empty())
    {
        node fro=q.front();
        q.pop();
        if(fro.fire==0&&(fro.x==0||fro.x==n-1||fro.y==0||fro.y==m-1))
        {
            cout<<fro.step+1<<endl;
            return ;
        }
        for(int i=0; i<4; i++)
        {
            int x=fro.x+dx[i];
            int y=fro.y+dy[i];
            if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&g[x][y]!='#')
            {
                node t;
                t.x=x,t.y=y,t.fire=fro.fire,t.step=fro.step+1;
                q.push(t);
                vis[x][y]=1;

            }

        }
    }
    cout<<"IMPOSSIBLE"<<endl;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        tot=0;
        cin>>n>>m;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                cin>>g[i][j];
                if(g[i][j]=='J')
                {
                    s.x=i,s.y=j,s.step=0,s.fire=0;
                }
                if(g[i][j]=='F')
                {
                    f.x=i;
                    f.y=j;
                    f.fire=1;
                    huo[tot++]=f;
                }
            }
        }
        bfs();
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值