Fire! UVA - 11624

12 篇文章 0 订阅
Joe works in a maze. Unfortunately, portions of the maze have
caught on re, and the owner of the maze neglected to create a re
escape plan. Help Joe escape the maze.
Given Joe's location in the maze and which squares of the maze
are on re, you must determine whether Joe can exit the maze before
the re reaches him, and how fast he can do it.
Joe and the re each move one square per minute, vertically or
horizontally (not diagonally). The re spreads all four directions
from each square that is on re. Joe may exit the maze from any
square that borders the edge of the maze. Neither Joe nor the re
may enter a square that is occupied by a wall.
Input
The rst line of input contains a single integer, the number of test
cases to follow. The rst 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 re
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
re 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

题意

给你个迷宫,F代表着火点,J代表人,#代表墙壁,.代表路,每一秒火和人都可以上下左右移动,火先移动,问最少多长时间走出迷宫

思路

bfs,卡了很长时间,需要注意两点,一是可能有多个着火点,二是要清空人走的位置。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <cstdio>
using namespace std;
const int inf = 0x3f3f3f3f;
struct node
{
    int x,y,step;

};
int n,m;
int dx[] = {-1,0,0,1};
int dy[] = {0,-1,1,0};
int jx,jy,fx,fy;
int vis[1100][1100];
int t[1100][1100];
char mp[1100][1100];
queue<node> J;
queue<node> F;
int bfs()
{
    while(!F.empty())
    {
        node u = F.front();
        F.pop();
        node v;
        for(int i=0;i<4;i++)
        {
            int tx = u.x+dx[i];
            int ty = u.y+dy[i];
            if(tx<1||tx>n||ty<1||ty>m||mp[tx][ty]=='#')
                continue;
            if(!vis[tx][ty])
            {
                vis[tx][ty] = 1;
                v.x = tx;
                v.y = ty;
                v.step = u.step+1;
                t[tx][ty] = t[u.x][u.y]+1;
                F.push(v);
            }
        }
    }
    memset(vis,0,sizeof(vis));
    while(!J.empty())
    {
        node u=J.front();
        J.pop();
        vis[u.x][u.y] = 1;
        if(u.x == 1||u.x==n||u.y==1||u.y==m)
            return u.step+1;
        node v;
        for(int i = 0;i<4;i++)
        {
            int tx = u.x+dx[i];
            int ty = u.y+dy[i];
            if(tx<1||ty<1||tx>n||ty>m||mp[tx][ty]=='#'||mp[tx][ty]=='F')
                continue;
            if(u.step+1>=t[tx][ty]) continue;
            if(!vis[tx][ty])
            {
                vis[tx][ty]  =1;
                v.x = tx;
                v.y = ty;
                v.step = u.step + 1;
                J.push(v);
            }
        }
    }
    return -1;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        node tmp;
        while(!J.empty()) J.pop();
        while(!F.empty()) F.pop();
        memset(vis,0,sizeof(vis));

        for(int i=1;i<=n;i++)
        {
            getchar();
            for(int j=1;j<=m;j++)
            {
                t[i][j] = inf;
                scanf("%c",&mp[i][j]);
                if(mp[i][j]=='J')
                {
                    tmp.x=i;
                    tmp.y=j;
                    tmp.step=0;
                    J.push(tmp);
                }
                if(mp[i][j]=='F')
                {
                    tmp.x=i;
                    tmp.y=j;
                    tmp.step = 0;
                    vis[i][j] = 1;
                    t[i][j] = 0;
                    F.push(tmp);
                }
            }
        }

        int ans = bfs();
        if(ans==-1)
            cout<<"IMPOSSIBLE"<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值