博客
关于我
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/

    你可能感兴趣的文章
    localhost:5000在MacOS V12(蒙特利)中不可用
    查看>>
    mac mysql 进程_Mac平台下启动MySQL到完全终止MySQL----终端八步走
    查看>>
    Mac OS 12.0.1 如何安装柯美287打印机驱动,刷卡打印
    查看>>
    MangoDB4.0版本的安装与配置
    查看>>
    Manjaro 24.1 “Xahea” 发布!具有 KDE Plasma 6.1.5、GNOME 46 和最新的内核增强功能
    查看>>
    mapping文件目录生成修改
    查看>>
    MapReduce程序依赖的jar包
    查看>>
    mariadb multi-source replication(mariadb多主复制)
    查看>>
    MariaDB的简单使用
    查看>>
    MaterialForm对tab页进行隐藏
    查看>>
    Member var and Static var.
    查看>>
    memcached高速缓存学习笔记001---memcached介绍和安装以及基本使用
    查看>>
    memcached高速缓存学习笔记003---利用JAVA程序操作memcached crud操作
    查看>>
    Memcached:Node.js 高性能缓存解决方案
    查看>>
    memcache、redis原理对比
    查看>>
    memset初始化高维数组为-1/0
    查看>>
    Metasploit CGI网关接口渗透测试实战
    查看>>
    Metasploit Web服务器渗透测试实战
    查看>>
    Moment.js常见用法总结
    查看>>
    MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
    查看>>