• 广度搜索解决迷宫问题


    一 问题描述

    用一个二维数组表示一个迷宫,其中 1 表示墙壁,0 表示可以走的路,只能横着走或竖着走,不能斜着走,编写程序,找出从左上角到右下角的最短路径

    int maze[5][5] = {

        0, 1, 0, 0, 0,

        0, 1, 0, 1, 0,

        0, 0, 0, 0, 0,

        0, 1, 1, 1, 0,

        0, 0, 0, 1, 0,

    };

    二 输入和输出

    1 输入

    一个 5 × 5 的二维数组,表示一个迷宫。数据保证有唯一解。

    2 输出

    左上角到右下角的最短路径。

    三 输入和输出样例

    1 输入样例

    0 1 0 0 0

    0 1 0 1 0

    0 0 0 0 0

    0 1 1 1 0

    0 0 0 1 0

    2 输出样例

    (0, 0)

    (1, 0)

    (2, 0)

    (2, 1)

    (2, 2)

    (2, 3)

    (2, 4)

    (3, 4)

    (4, 4)

    四 算法设计

    可以使用广度优先搜索解决。定义方向数组 dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}

    1 定义一个队列,将起点(0,0)入队,标记已走过。

    2 如果队列不空,则队列出队。

    3 如果队头正好是目标(4,4),则退出。

    4 沿着 4 个方向搜索,如果该节点未出边界,未走过且不是墙壁,则标记走过并入队,用前驱数组记录该节点。

    5 转向步骤 2。

    6 根据前驱数组输出从起点到终点的最短路径。

    五 代码

    1. package com.platform.modules.alg.alglib.poj3984;
    2. import java.util.LinkedList;
    3. import java.util.Queue;
    4. public class Poj3984 {
    5. boolean mp[][] = new boolean[5][5];
    6. boolean vis[][] = new boolean[5][5];
    7. int dir[][] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    8. public String output = "";
    9. private node pre[][] = new node[10][10];
    10. public Poj3984() {
    11. for (int i = 0; i < 10; i++) {
    12. for (int j = 0; j < 10; j++) {
    13. pre[i][j] = new node();
    14. }
    15. }
    16. }
    17. public String cal(String input) {
    18. String[] line = input.split("\n");
    19. for (int i = 0; i < 5; i++) {
    20. String[] words = line[i].split(" ");
    21. for (int j = 0; j < 5; j++) {
    22. mp[i][j] = words[j].equals("1");
    23. }
    24. }
    25. bfs();
    26. node ed = new node();
    27. ed.x = ed.y = 4;
    28. print(ed);
    29. return output;
    30. }
    31. void bfs() {
    32. Queue que = new LinkedList<>(); // 创建一个普通队列(先进先出)
    33. node st = new node();
    34. st.x = 0;
    35. st.y = 0;
    36. que.add(st);
    37. vis[0][0] = true;
    38. while (!que.isEmpty()) {
    39. node now = que.peek();
    40. que.poll();
    41. if (now.x == 4 && now.y == 4)
    42. return;
    43. for (int i = 0; i < 4; i++) {
    44. node next = new node();
    45. next.x = now.x + dir[i][0];
    46. next.y = now.y + dir[i][1];
    47. if (next.x >= 0 && next.x < 5 && next.y >= 0 && next.y < 5 && !mp[next.x][next.y] && !vis[next.x][next.y]) {
    48. vis[next.x][next.y] = true;
    49. que.add(next);
    50. pre[next.x][next.y] = now;
    51. }
    52. }
    53. }
    54. }
    55. void print(node cur) {
    56. if (cur.x == 0 && cur.y == 0) {
    57. output += "(0,0)\n";
    58. return;
    59. }
    60. print(pre[cur.x][cur.y]); // 逆序输出
    61. output += "(" + cur.x + "," + cur.y + ")\n";
    62. }
    63. }
    64. class node {
    65. int x;
    66. int y;
    67. }

    六 测试

     

  • 相关阅读:
    掌握雅思写作:任务 2(在 7 小时内达到 7+ 级)
    SpringBoot_启动原理分析
    【部署笔记】docker-compose 安装mongodb,一直重启 + 无法连接
    【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
    【C++】初阶模板
    006-JavaSE基础巩固练习:while循环结构的练习
    php发送get、post请求的6种方法简明总结?
    Linux 命令行——文件查找:locate、find
    华卓荣登「2024数商典型应用场景“乘数榜”」
    JAVA多线程基础学习三:volatile关键字
  • 原文地址:https://blog.csdn.net/chengqiuming/article/details/126440756