UVA - 11624 Fire! bfs

题目描述

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

输入

第一行输入包含一个整数,即测试次数
每个测试用例的第一行包含两个
整数R和C,用空格分隔,1≤R,C≤1000
下面R行中,每一行都包含C个字符,以及每个字符是以下之一:
# 代表墙
. 代表空地,火和乔是可通行的
J 乔在迷宫中最初的位置,火和乔是可通行的
F 代表火
在每组测试中只有一个J

输出

对于每个测试用例,如果在火蔓延的时候烧到了乔,则乔无法逃出迷宫,输出'IMPOSSIBLE'如果乔能逃出迷宫,则输出乔最快可以在几分钟内安全逃出迷宫,每组输出占一行

样例输入

2
4 4
####
#JF#
#..#
#..#
3 3
###
#J.
#.F

样例输出

3
IMPOSSIBLE

思路:

bfs,开始可以先将火源添加到队列中,然后添加初始位置,然后进行bfs。

可以将火源经过的'.'位置设置成'F'。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn=1005;
int t;
int n,m;
char a[maxn][maxn];
int vis[maxn][maxn];
int dri[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct node
{
    int x,y;
    int step;
    int ele;
};
int sx,sy;
vector<int>fx;
vector<int>fy;
int bfs ()
{
    queue<node>q;
    node now,next;
    int Size=fx.size();
    for (int i=0;i<Size;i++)
    {
        now.step=0;
        now.x=fx[i]; now.y=fy[i];
        now.ele=0;
        q.push(now);
        vis[now.x][now.y]=1;
    }
    vis[sx][sy]=1;
    now.step=0;
    now.x=sx; now.y=sy;
    now.ele=1;
    q.push(now);
    while (!q.empty())
    {

        now=q.front();q.pop();
        if(now.x==n-1||now.y==m-1||now.x==0||now.y==0)
        {
            if(now.ele==1)
            {
                return now.step+1;
            }
        }
        for (int i=0;i<4;i++)
        {
            next=now;
            next.x+=dri[i][0];
            next.y+=dri[i][1];
            next.step++;
            if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m)
            {
                if(a[next.x][next.y]=='.')
                {
                    if(next.ele==0)
                    {
                        a[next.x][next.y]='F';
                        q.push(next);
                    }
                    else
                    {
                        if(!vis[next.x][next.y])
                        {
                            vis[next.x][next.y]=1;
                            q.push(next);
                        }
                    }
                }
            }
        }
    }
    return -1;
}
int main()
{
    scanf("%d",&t);
    while (t--)
    {
        fx.clear(); fy.clear();
        memset (vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        getchar();
        for (int i=0;i<n;i++)
        {
            for (int j=0;j<m;j++)
            {
                scanf("%c",&a[i][j]);
                if(a[i][j]=='J')
                {
                    sx=i; sy=j;
                }
                else if(a[i][j]=='F')
                {
                    fx.push_back(i);
                    fy.push_back(j);
                }
            }
            getchar();
        }
        int ans=bfs();
        if(ans==-1)
            printf("IMPOSSIBLE\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/87452077
今日推荐