UVA 11624 Fire!

本文介绍了一种使用广度优先搜索(BFS)解决迷宫中人物躲避火焰并寻找最快逃生路径的问题。通过同时模拟人物与火焰的移动,判断人物是否能在火焰蔓延到其位置前到达迷宫边界。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Joe works in a maze. Unfortunately, portions of the maze havecaught on fire, and the owner of the maze neglected to create a fireescape plan. Help Joe escape the maze.Given Joe’s location in the maze and which squares of the mazeare on fire, you must determine whether Joe can exit the maze beforethe fire reaches him, and how fast he can do it.Joe and the fire each move one square per minute, vertically orhorizontally (not diagonally). The fire spreads all four directionsfrom each square that is on fire. Joe may exit the maze from anysquare that borders the edge of the maze. Neither Joe nor the firemay enter a square that is occupied by a wall.

Input

The first line of input contains a single integer, the number of testcases to follow. The first line of each test case contains the twointegers R and C, separated by spaces, with 1 ≤ R, C ≤ 1000. Thefollowing R lines of the test case each contain one row of the maze. Each of these lines contains exactlyC 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 fireThere will be exactly one J in each test case.OutputFor each test case, 

output

 a single line containing ‘IMPOSSIBLE’ if Joe cannot exit the maze before thefire 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即可

/*************************************************************************
	> File Name: Fire.cpp
	> Author: Zhanghaoran0
	> Mail: chiluamnxi@gmail.com
	> Created Time: 2015年08月15日 星期六 09时48分20秒
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

int T;
int R, C;
char map[1005][1005];
int sx = -1, sy = -1;
struct node{
    int x;
    int y;
    bool form;
    int step;
}q[1000010];

bool flag[1005][1005];
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};

int bfs(){
    int head = 0;
    int tail = 0;

    memset(flag, false, sizeof(flag));
    for(int i = 0; i < R; i ++)
        for(int j = 0; j < C; j ++)
            if(map[i][j] == 'F'){
                q[tail].x = i;
                q[tail].y = j;
                q[tail].form = false;
                q[tail].step = 0;
                tail ++;
            }   

    q[tail].x = sx;
    q[tail].y = sy;
    q[tail].step = 0;
    q[tail].form = true;
    tail ++;
    while(head < tail){
        if(!q[head].form){
            for(int i = 0; i < 4; i ++){
                int tempx = q[head].x + dx[i];
                int tempy = q[head].y + dy[i];
                if(tempx < 0 || tempx >= R || tempy < 0 || tempy >= C || map[tempx][tempy] == '#' || map[tempx][tempy] == 'F')
                    continue;
                map[tempx][tempy] = 'F';
                q[tail].x = tempx;
                q[tail].y = tempy;
                q[tail].form = false;
                q[tail].step = q[head].step + 1;
                tail ++;
            } 
        }

        else{
            for(int i = 0; i < 4; i ++){
                int tempx = q[head].x + dx[i];
                int tempy = q[head].y + dy[i];
                if(flag[tempx][tempy] || map[tempx][tempy] == '#' || map[tempx][tempy] == 'F')
                    continue;
                if(tempx < 0 || tempx >= R || tempy < 0 || tempy >= C)
                    return q[head].step + 1;
                flag[tempx][tempy] = true;
                q[tail].x = tempx;
                q[tail].y = tempy;
                q[tail].form = true;
                q[tail].step = q[head].step + 1;
                tail ++;
            }
        }
        head ++;
    }
    return 0;
}


int main(void){
    cin >> T;
    while(T --){
        cin >> R >> C;
        sx = -1;
        sy = -1;
        int ans = 0;
        for(int i = 0; i < R; i ++){
            scanf("%s", map[i]);
            if(sx == -1 && sy == -1){
                for(int j = 0; j < C; j ++)
                    if(map[i][j] == 'J'){
                        sx = i;
                        sy = j;
                        break;
                    }
            }
        }
        ans = bfs();
        if(ans)
            printf("%d\n", ans);
        else 
            printf("IMPOSSIBLE\n");
    }
    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值