Fire! UVA - 11624 两种方法

两个队列的bfs
火势蔓延的地方joe不能走
一种方法是每次joe移动前将火势蔓延一次,另一种是将火势蔓延到某点的时间预处理出来,只要joe小于这个时间到达,就可以到达
1.题目有多个起火点
2.第二个方法有一个坑,需要将joe走过的标记,不然就会无限循环

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<queue>
#include<string>
using namespace std ;
char mp[1002][1002];
int vis[1002][1002];
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
int m,n;
struct joe
{
    int x,y,step;
} s,u,v;
struct fire
{
    int x,y;
} tmp,now;
queue<fire> qu;
bool chck(int x,int y)
{
    if(x>=0&&y>=0&&x<n&&y<m&&mp[x][y]=='.')
    {
        return 1;
    }
    return 0;
}
void fextend()
{
    int n=qu.size();
    while(n--)
    {
        now=qu.front();
        qu.pop();
        for(int i=0; i<4; i++)
        {
            tmp.x=now.x+dir[i][0];
            tmp.y=now.y+dir[i][1];
            if(chck(tmp.x,tmp.y)&&!vis[tmp.x][tmp.y])
            {
                mp[tmp.x][tmp.y]='F';
                vis[tmp.x][tmp.y]=1;
                qu.push(tmp);
            }
        }
    }
}
queue<joe> q;
void bfs(int x,int y)
{
    u.x=x,u.y=y,u.step=0;
    q.push(u);
    while(!q.empty())
    {
        fextend();
        int le=q.size();
        while(le--)
        {
            u=q.front();
            q.pop();
            // printf("%d %d---\n",u.x,u.y);
            if(u.x<0||u.x>=n||u.y<0||u.y>=m)
            {
                printf("%d\n",u.step);
                return ;
            }
            for(int i=0; i<4; i++)
            {
                v.x=u.x+dir[i][0];
                v.y=u.y+dir[i][1];
                //  printf("%d %d %d\n",v.x,v.y,vis[v.x][v.y]);
                if(vis[v.x][v.y])
                    continue;
                vis[v.x][v.y]=1;
                v.step=u.step+1;
                q.push(v);
            }
        }

    }
    printf("IMPOSSIBLE\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        while(!qu.empty()) qu.pop();
        while(!q.empty()) q.pop();
        int i,sx,sy,j;
        scanf("%d%d",&n,&m);
        for(i=0; i<n; i++)
        {
            scanf("%s",&mp[i]);
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(mp[i][j]=='J')
                {
                    sx=i;
                    sy=j;
                    vis[i][j]=1;
                }
                if(mp[i][j]=='#')
                    vis[i][j]=1;
                if(mp[i][j]=='F')
                {
                    vis[i][j]=1;
                    tmp.x=i;
                    tmp.y=j;
                    qu.push(tmp);
                }
            }
        }
        bfs(sx,sy);
    }
    return 0 ;
}

第二种方法简单点

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<queue>
#include<string>
using namespace std ;
char mp[1002][1002];
int vis[1002][1002],timeF[1002][1002];
int dir[4][2] = {0,1,0,-1,1,0,-1,0};
int m,n;
struct joe
{
    int x,y,step;
} s,u,v;
struct fire
{
    int x,y,step;
} tmp,now;
queue<fire> qu;
bool chck(int x,int y)
{
    if(x>=0&&y>=0&&x<n&&y<m&&mp[x][y]=='.')
    {
        return 1;
    }
    return 0;
}
void fextend()
{
    while(!qu.empty())
    {
        now=qu.front();
        qu.pop();
        for(int i=0; i<4; i++)
        {
            tmp.x=now.x+dir[i][0];
            tmp.y=now.y+dir[i][1];
            tmp.step=now.step+1;
            if(chck(tmp.x,tmp.y)&&!vis[tmp.x][tmp.y])
            {
                mp[tmp.x][tmp.y]='F';
                timeF[tmp.x][tmp.y]=tmp.step;
                qu.push(tmp);
            }
        }
    }
}
queue<joe> q;
void bfs(int x,int y)
{
    u.x=x,u.y=y,u.step=0;
    q.push(u);
    fextend();
    while(!q.empty())
    {

        u=q.front();
        q.pop();
        if(u.x<0||u.x>=n||u.y<0||u.y>=m)
        {
            printf("%d\n",u.step);
            return ;
        }
        for(int i=0; i<4; i++)
        {
            v.x=u.x+dir[i][0];
            v.y=u.y+dir[i][1];
            if(vis[v.x][v.y])
                continue;
            if(mp[v.x][v.y]=='F'&&(u.step+1)>=timeF[v.x][v.y])
                continue;
            vis[v.x][v.y]=1;
            v.step=u.step+1;
            q.push(v);
        }


    }
    printf("IMPOSSIBLE\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        while(!qu.empty()) qu.pop();
        while(!q.empty()) q.pop();
        int i,sx,sy,j;
        scanf("%d%d",&n,&m);
        for(i=0; i<n; i++)
        {
            scanf("%s",&mp[i]);
        }
        for(i=0; i<n; i++)
        {
            for(j=0; j<m; j++)
            {
                if(mp[i][j]=='J')
                {
                    sx=i;
                    sy=j;
                    vis[i][j]=1;
                }
                if(mp[i][j]=='#')
                    vis[i][j]=1;
                if(mp[i][j]=='F')
                {
                    vis[i][j]=1;
                    tmp.x=i;
                    tmp.y=j;
                    tmp.step=0;
                    qu.push(tmp);
                }
            }
        }
        bfs(sx,sy);
    }
    return 0 ;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值