HDU1429 - 胜利大逃亡(续)

本文介绍如何使用C++实现广度优先搜索(BFS)算法来解决迷宫问题,通过设置状态空间和利用队列进行遍历,最终找到从起点到终点的最短路径。

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

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <queue>
#include <vector>
#include <algorithm>
#include <stack>
#include <cmath>
#define maxn 18
#define eps (1e-7)
typedef long long ll;
using namespace std;
char Map[25][25];
bool Hash[25][25][1030];
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};//UDLR
int n, m, t;
struct node {
    int x, y, key, state;
    node(int x = 0, int y = 0, int k = 0, int st = 0) : x(x),y(y),key(k),state(st) {}
    bool ok() {
        if (x >= 0 && x < n && y >= 0 && y < m && Map[x][y] != '*') return true;
        return false;
    }
};
queue<node> Q;
int bfs() {
    while (!Q.empty()) {
        node tmp = Q.front();
        Q.pop();
        if (Map[tmp.x][tmp.y] == '^') {
            if (tmp.state < t) return tmp.state;
        }
        if (tmp.state >= t) break;
        for (int i = 0; i < 4; ++ i) {
            int nx = tmp.x + dx[i];
            int ny = tmp.y + dy[i];
            if (node(nx,ny,0,0).ok()) {
                //cout << nx << " " << ny << " " << node(nx,ny,0,0).x << " " << node(nx,ny,0,0).y << endl;
                if (Map[nx][ny] >= 'A' && Map[nx][ny] <= 'J') {
                    int key = Map[nx][ny] - 'A';
                    if (((tmp.key >> key) & 1) && !Hash[nx][ny][tmp.key]) {
                        Hash[nx][ny][tmp.key] = 1;
                        Q.push(node(nx,ny,tmp.key,tmp.state+1));
                    }
                }
                else if (Map[nx][ny] >= 'a' && Map[nx][ny] <= 'j') {
                    int key = Map[nx][ny] - 'a';
                    key = (tmp.key |(1 << key));
                    if (!Hash[nx][ny][key]) {
                        Hash[nx][ny][key] = 1;
                        Q.push(node(nx,ny,key,tmp.state+1));
                    }
                }
                else {
                    if (!Hash[nx][ny][tmp.key]) {
                        Hash[nx][ny][tmp.key] = 1;
                        Q.push(node(nx,ny,tmp.key,tmp.state+1));
                    }
                }
            }
        }
    }
    return -1;
}
int main() {
    while (scanf("%d%d%d", &n, &m, &t) != EOF) {
        node st;
        memset(Hash, 0, sizeof(Hash));
        while (!Q.empty()) Q.pop();
        for (int i = 0; i < n; ++ i) {
            scanf("%s", Map[i]);
            for (int j = 0; j < m; ++ j) {
                if (Map[i][j] == '@') {
                    st.x = i;
                    st.y = j;
                    st.key = 0;
                    st.state = 0;
                }
            }
        }
        Hash[st.x][st.y][st.key] = 1;
        Q.push(st);
        cout << bfs() << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值