[ACM] UVa 11624 Fire!

本文介绍了一个使用C++实现的迷宫逃生模拟程序。该程序通过搜索算法帮助虚拟角色Joe避开火灾,找到最近的安全出口。文章详细展示了如何读取迷宫地图、如何利用队列进行火势蔓延模拟以及Joe寻找逃生路径的过程。

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

#include<iostream>
#include<queue>
#include<cstring>

using namespace std;
const int MAXDATA = 1005;
const int NOTV = -1;
const int OPR[4][2] = { 0, 1, 0, -1, 1, 0, -1, 0};

struct d2Pos {
    int x, y, d;
};

int X__, Y__, R__;
char g[MAXDATA][MAXDATA];
int fire[MAXDATA][MAXDATA],
    joev[MAXDATA][MAXDATA];

queue<d2Pos> fireQ;
d2Pos joe;

void readg() {
    R__ = 0;
    while (!fireQ.empty())
        fireQ.pop();

    cin >> X__ >> Y__;
    for (int i = 0; i < X__; i++) {
        for (int j = 0; j < Y__; j++) {
            char t;
            cin >> t;
            g[i][j] = t;
            joev[i][j] = NOTV;
            switch (t) {
            case 'J':
                joev[i][j] = 0;
                fire[i][j] = NOTV;
                joe.x = i; joe.y = j; joe.d = 0;
            case '.':
                fire[i][j] = NOTV;
                break;
            case 'F':
                fire[i][j] = 0;
                d2Pos t1;
                t1.x = i; t1.y = j; t1.d = 0;
                fireQ.push(t1);
            }
        }
    }
}

bool inmaze(d2Pos t) {
    return (t.x >= 0 && t.y >= 0 && t.x < X__ && t.y < Y__);
}

void Search_Fire() {
    d2Pos t, v;

    while (!fireQ.empty()) {
        t = fireQ.front();
        fireQ.pop();

        for (int i = 0; i < 4; i++) {
            v.d = t.d + 1;
            v.x = t.x + OPR[i][0];
            v.y = t.y + OPR[i][1];

            if (!inmaze(v))
                continue;
            if (g[v.x][v.y] == '#')
                continue;
            if (fire[v.x][v.y] != NOTV)
                continue;

            fireQ.push(v);
            fire[v.x][v.y] = v.d;

        }
    }
}

void Search_Joe() {
    queue<d2Pos> Q;
    Q.push(joe);

    d2Pos t, v;

    while (!Q.empty()) {
        t = Q.front();
        Q.pop();

        if (t.x == 0 || t.y == 0 || t.x == X__ - 1 || t.y == Y__ - 1) {
            R__ = t.d + 1;
            return;
        }

        for (int i = 0; i < 4; i++) {
            v.d = t.d + 1;
            v.x = t.x + OPR[i][0];
            v.y = t.y + OPR[i][1];

            if (!inmaze(v))
                continue;
            if (g[v.x][v.y] == '#')
                continue;
            if (fire[v.x][v.y] != NOTV && v.d >= fire[v.x][v.y])
                continue;
            if (joev[v.x][v.y] != -1)
                continue;

            Q.push(v);
            joev[v.x][v.y] = v.d;

        }
    }
}

int main() {
    int T;
    cin >> T;
    while (T--) {
        readg();
        Search_Fire();
        Search_Joe();
        if (R__ == 0)
            cout << "IMPOSSIBLE" << endl;
        else
            cout << R__ << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值