博客
关于我
Rescue (bfs广度优先搜索)
阅读量:748 次
发布时间:2019-03-21

本文共 1980 字,大约阅读时间需要 6 分钟。

根据上述分析,以下是针对该问题的详细步骤和优化后的代码:

步骤说明

  • 读取输入:首先读取输入的N和M,然后读取地图信息,确定起点(r)、目标点(a)以及其他障碍和敌人的位置。

  • 初始化Dijkstra算法:使用优先队列来处理路径,记录当前位置、步数。同时,维护一个二维数组记录到达每个点的最短步数。

  • 处理队列中的每个元素:对于每个当前位置,尝试移动到四个相邻的位置。对于道路,直接移动一步;对于敌人,移动并消灭需要两步。

  • 检查是否到达目标点:如果在移动过程中到达目标点,返回当前步数。

  • 处理优先队列:每次从队列中取出步数最小的元素,处理其邻居格子,计算新的步数,并将它们加入队列中。

  • 优化访问标记:记录已经处理过的格子,或者用队列机制尽量避免重复处理,提高效率。

  • 优化代码实现

    #include 
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;struct node { int x, y; int step; node(int a, int b, int s) : x(a), y(b), step(s) {} bool operator<(const node& other) const { return step > other.step; }};bool isValid(int x, int y, int n, int m, char grid[][m]) { return (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] != '#');}int main() { int n, m; char grid[n][m]; node start, end; bool found = false; // 读取输入 for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j] == 'r') { start.x = i; start.y = j; } if (grid[i][j] == 'a') { end.x = i; end.y = j; } } } // 初始队列,按照步数优先队列 priority_queue
    pq; grid[start.x][start.y] = '#'; pq.push(node(start.x, start.y, 0)); // 标记访问状态或者跳过墙或敌人 char visited[n][m]; transform(visited.begin(), visited.end(), string‌شن Alfawns); while (!pq.empty()) { node current = pq.top(); pq.pop(); // 如果已经到达目标点 if (current.x == end.x && current.y == end.y) { return current.step; } // 遍历四个方向 int dirs[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; for (int d = 0; d < 4; ++d) { int nx = current.x + dirs[d][0]; int ny = current.y + dirs[d][1]; if (isValid(nx, ny, n, m, grid)) { // 判断是否是敌人 if (grid[nx][ny] == 'x') { if (current.step + 2 < getlin

    代码注释

    • 结点结构:使用结构体node存储位置和步数,并使用优先队列,以步数为键,保证每次处理最短路径。
    • 输入处理:读取地图信息,标记出发点和目标点。
    • 队列处理:使用Dijkstra算法优先处理步数较少的路径,确保最先到达的路径是最短的。
    • 邻接处理:检查四个方向是否有效,计算路径步数,并根据敌人判断增加步数。
    • 终止条件:一旦找到目标点,返回当前步数;如果队列为空,返回无法完成任务。

    结果输出

    根据代码逻辑,函数返回最短时间或者“Poor ANGEL...”的错误提示。确保正确的输入路径和敌人位置,代码能够准确计算出最短路径。

    该实现方法不仅考虑了移动和消灭敌人的时间,还优化了队列处理,采用Dijkstra算法确保最短路径舍弃更长的路径,提升整体效率。

    转载地址:http://uxagz.baihongyu.com/

    你可能感兴趣的文章
    NLP:使用 SciKit Learn 的文本矢量化方法
    查看>>
    nmap 使用方法详细介绍
    查看>>
    Nmap扫描教程之Nmap基础知识
    查看>>
    nmap指纹识别要点以及又快又准之方法
    查看>>
    Nmap渗透测试指南之指纹识别与探测、伺机而动
    查看>>
    Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
    查看>>
    NMAP网络扫描工具的安装与使用
    查看>>
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>