UVA11624 Fire BFS

题解

简单BFS,先对火(可能不止一个)进行BFS求出每个格子最快蔓延的时间,然后再对人进行BFS即可,居然没留意到到了边界之后步数还要+1,WA之后改了好久…

代码

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define MAX 100000
#define LL long long
int cas=1,T;
int n,m;
char mapp[1001][1001];
int vis[1001][1001];
int firetime[1001][1001];
struct Node
{
    int x;
    int y;
    int step;
};
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool check(int x,int y)
{
    if (x<0 || y<0 || x>=n || y>=m)
        return true;
    if (vis[x][y] || mapp[x][y]=='#'  )
        return true;
    return false;
}
bool checkpeople(int x,int y,int t)
{
   if (x<0 || y<0 || x>=n||y>=m)
       return true;
   if (vis[x][y] || mapp[x][y]=='#' || mapp[x][y]=='F')
       return true;
   if (firetime[x][y] <= t)
       return true;
   return false;
}
int main()
{
    scanf("%d",&T);
    while (T--)
    {
        queue<Node> que;
        Node pe,fi;
        scanf("%d%d",&n,&m);
        for (int i = 0;i<n;i++)
            scanf("%s",&mapp[i]);
        for (int i = 0;i<n;i++)
            for (int j = 0;j<m;j++)
                firetime[i][j]=999999;

        for (int i = 0;i<n;i++)
            for (int j = 0;j<m;j++)
            {
                if (mapp[i][j]=='J')
                {
                    pe.x=i;
                    pe.y=j;
                    pe.step=0;
                }
                if (mapp[i][j]=='F')
                {
                    fi.x=i;
                    fi.y=j;
                    fi.step=0;
                    que.push(fi);
                }
            }

          while (!que.empty())
          {
                Node q = que.front();
                que.pop();
                for (int i = 0;i<4;i++)
                {
                        Node t = q;
                        t.x=q.x+dir[i][0];
                        t.y=q.y+dir[i][1];
                        t.step=q.step+1;
                        if (check(t.x,t.y))
                            continue;

                        if (t.step<firetime[t.x][t.y])
                         firetime[t.x][t.y]=t.step;
                        vis[t.x][t.y]=1;
                        que.push(t);
                }
            }
         memset(vis,0,sizeof(vis));

        while (!que.empty())
            que.pop();
        //memset(vis,0,sizeof(vis));

        que.push(pe);
        vis[pe.x][pe.y]=1;
        int flag = 0;
        while (!que.empty())
        {
            Node q = que.front();
            que.pop();
            if (q.x==0 || q.y==0 || q.x==n-1 || q.y==m-1)
            {
               flag = 1;
               printf("%d\n",q.step+1);
               break;
            }
            for (int i = 0;i<4;i++)
            {
                Node t = q;
                t.x=q.x+dir[i][0];
                t.y=q.y+dir[i][1];
                t.step++;
                if (checkpeople(t.x,t.y,t.step))
                    continue;
                vis[t.x][t.y]=1;
                que.push(t);
            }
        }
        if (!flag)
            printf("IMPOSSIBLE\n");
        memset(vis,0,sizeof(vis));
        memset(firetime,0,sizeof(firetime));
        memset(mapp,0,sizeof(mapp));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值