Sicily 1754. 逃离洞穴

这是一个关于洞穴探险的问题,04级ZEH和YYR面临毒气喷射口的威胁。当洞内的毒气开始喷射时,YYR需要在毒气扩散到其位置前尽快逃离。题目给出洞穴地图,包括障碍物、洞口、毒气喷射口和YYR的位置,要求计算最快逃离时间。如果无法逃离,则输出警告信息。

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

1754. 逃离洞穴

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

04级ZEH和YYR到一个洞穴探险,其中YYR进入洞穴探险,而ZEH在洞外附近利用自发研制的X-1探测器监测洞穴的喷气口是否开始喷射毒气。洞穴里面有若干个毒气喷射口,已知毒气的扩散速度是每单位时间向上下左右扩散一格,而YYR的逃逸速度也是每单位时间向上下左右移动一格。所有的毒气喷射口都是同时喷发的,当监测到喷射时,ZEH就马上通知YYR逃离,YYR必须在不遇到毒气的情况下尽快逃离洞穴。亲爱的师弟师妹,你们可以帮他们计算一下YYR最快逃离洞穴的时间吗?

Input

洞穴地图用矩阵表示,符号. 表示通路,符号X表示障碍物,符号E表示洞口,符号D表示毒气喷射口所在地,符号P表示YYR当前的位置。输入首先是两个整数m, n(2<=m,n<=1000)表示矩阵的大小。接下来是m行n列的矩阵图。矩阵描述之外的地方都是障碍物且只有一个逃离的洞口,但可能有多个毒气喷射口,且扩散到洞口的毒气立即消散。输入有多组数据,当m,n是0 0时表示输入结束。

Output

如果YYR能够顺利逃离洞穴,则输出最快逃离洞口的时间,否则输出“YYR is extremely dangerous!”

Sample Input

5 10
XXXXX..XXX
XXXE...XXX
XX.......X
X......P.X
XD.....XXX
5 10
XXXXX..XXX
XXXE...XXX
XX.......X
X......P.X
X.D....XXX
0 0

Sample Output

6

YYR is extremely dangerous!

// Problem#: 1754
// Submission#: 3326115
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
using namespace std;

const int MAX_W = 1005;

bool visGas[MAX_W][MAX_W];
bool visP[MAX_W][MAX_W];
char G[MAX_W][MAX_W];
int H, W;
int Ei, Ej;
queue<pair<int, int> > gas;
queue<pair<int, int> > q;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int step;

bool isValidGas(int i, int j) {
    return 0 <= i && i < H && 0 <= j && j < W && !visGas[i][j] && (G[i][j] == '.' || G[i][j] == 'P');
}

bool isValidP(int i, int j) {
    return 0 <= i && i < H && 0 <= j && j < W && !visP[i][j] && G[i][j] == '.';
}

void GAS() {
    int s = gas.size();
    while (s--) {
        pair<int, int> p = gas.front();
        gas.pop();
        for (int i = 0; i < 4; i++) {
            int nextI = p.first + dir[i][0];
            int nextJ = p.second + dir[i][1];
            if (isValidGas(nextI, nextJ)) {
                G[nextI][nextJ] = 'D';
                visGas[nextI][nextJ] = true;
                gas.push(make_pair(nextI, nextJ));
            }
        }
    }
}

bool BFS() {
    step = 0;
    while (!q.empty()) {
        int s = q.size();
        GAS();
        step++;
        while (s--) {
            pair<int, int> p = q.front();
            q.pop();
            //if (G[p.first][p.second] == 'D') continue;
            for (int i = 0; i < 4; i++) {
                int nextI = p.first + dir[i][0];
                int nextJ = p.second + dir[i][1];
                if (nextI == Ei && nextJ == Ej) return true;
                if (isValidP(nextI, nextJ)) {
                    visP[nextI][nextJ] = true;
                    q.push(make_pair(nextI, nextJ));
                }
            }
        }
    }
    return false;
}

int main() {

    //std::ios::sync_with_stdio(false);

    while (1) {
        cin >> H >> W;
        if (H == 0 && W == 0) break;
        for (int i = 0; i < H; i++) {
            cin >> G[i];
            for (int j = 0; j < W; j++) {
                if (G[i][j] == '.') visP[i][j] = visGas[i][j] = false;
                if (G[i][j] == 'D') {
                    gas.push(make_pair(i, j));
                    visGas[i][j] = true;
                }
                if (G[i][j] == 'P') {
                    q.push(make_pair(i, j));
                    visP[i][j] = true;
                }
                if (G[i][j] == 'E') {
                    Ei = i;
                    Ej = j;
                }
            }
        }
        if (BFS()) cout << step << endl;
        else cout << "YYR is extremely dangerous!" << endl;
        while (!gas.empty()) gas.pop();
        while (!q.empty()) q.pop();
    }


    //getchar();
    //getchar();
    
    return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值