UVA - 11624 Fire!

Fire!

Joe works in a maze. Unfortunately, portions of the maze have
caught on fire, and the owner of the maze neglected to create a fire
escape plan. Help Joe escape the maze.
Given Joe’s location in the maze and which squares of the maze
are on fire, you must determine whether Joe can exit the maze before
the fire reaches him, and how fast he can do it.
Joe and the fire each move one square per minute, vertically or
horizontally (not diagonally). The fire spreads all four directions
from each square that is on fire. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the fire
may enter a square that is occupied by a wall.
Input
The first line of input contains a single integer, the number of test
cases to follow. The first line of each test case contains the two
integers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. The
following R lines of the test case each contain one row of the maze. Each of these lines contains exactly
C characters, and each of these characters is one of:
• #, a wall
• ., a passable square
• J, Joe’s initial position in the maze, which is a passable square
• F, a square that is on fire
There will be exactly one J in each test case.
Output
For each test case, output a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before the
fire reaches him, or an integer giving the earliest time Joe can safely exit the maze, in minutes.
Sample Input
2
4 4

#JF#
#…#
#…#
3 3

#J.
#.F
Sample Output
3
IMPOSSIBLE

思路
开始给的火可能有很多!!!这道题开始我并不会写,没想到双BFS,我想用一个BFS解决,结果运行时间错误,后来,我同学告诉我说火是同时扩散的,然后我就想到了先用一次BFS进行火所到位置的所用最短时间打表,再对人用一次BFS,但是实际操作的时候还是出现了许多问题,我觉得这是我长时间不敲搜索导致的,下面是我写的时候出现的问题。
1.是我没有最先更新下一步的最短时间,导致最短时间一直是0,所以一直错。
2.是第一次对火BFFS打表所用的数组一定要用较大的值去遍历数组,因为,有些地方火可能到达不了而人可以,所以如果用0遍历数组的话会出现人到这个位置最短时间>=0的,而火到达这里的最短时间=0,这会导致人不能通过这里,所以不能用0.这个开始我并没有想到所以WA了好几次,然后看大佬的代码才明白过来.
3.是memset函数,这个函数比循环调用快,但是它只能对0,-1,或者对应类型的每个字节都是同一个数的时候也可以调用。

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <limits.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int dis[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char a[1010][1010];
int c[1010][1010],f[1010][1010];
const int INF = 0x0f0f0f0f;
void fun(int n,int m)
{
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++) a[i][j]=c[i][j];
}
typedef struct
{
    int x,y,ans;
}fire;
int main()
{
    std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
        memset(f,INF,sizeof(f));
        memset(c,0,sizeof(c));
        int n,m,arr=1;
        queue<fire >q1,q2;
        cin>>n>>m;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>a[i][j];
                c[i][j]=a[i][j];
                fire start;
                if(a[i][j]=='J') start.x=i,start.y=j,start.ans=0,q1.push(start);
                if(a[i][j]=='F') start.x=i,start.y=j,start.ans=0,q2.push(start),f[i][j]=0;
            }
        }
        while(q2.size())
        {
            fire b,d;
            b=q2.front();
            q2.pop();
            for(int i=0;i<4;i++)
            {
                d.x=b.x+dis[i][0];
                d.y=b.y+dis[i][1];
                d.ans=b.ans+1;
                if((d.x>=0&&d.x<n&&d.y>=0&&d.y<m)&&(a[d.x][d.y]=='.'||a[d.x][d.y]=='J'))
                {
                    q2.push(d);
                    f[d.x][d.y]=d.ans;
                    a[d.x][d.y]='#';
                }
            }
        }
        fun(n,m);
        while(q1.size())
        {
            fire b,d;
            b=q1.front();
            q1.pop();
            if(b.x==0||b.x==n-1||b.y==0||b.y==m-1)
            {
                arr=0;
                printf("%d\n",b.ans+1);
                break;
            }
            for(int i=0;i<4;i++)
            {
                d.x=b.x+dis[i][0];
                d.y=b.y+dis[i][1];
                d.ans=b.ans+1;
                if(d.x>=0&&d.x<n&&d.y>=0&&d.y<m&&a[d.x][d.y]=='.'&&d.ans<f[d.x][d.y])
                {
                    q1.push(d);
                    a[d.x][d.y]='#';
                }
            }
        }
        if(arr) printf("IMPOSSIBLE\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值