• 经典算法-----迷宫问题(栈的应用)


    目录

    前言

    迷宫问题

    算法思路

    1.栈的使用方法 

    ​编辑2.方向的定义

    代码实现

    栈的cpp代码:

    栈的头文件h代码:

    走迷宫代码:


    前言

            今天学习一种算法问题,也就是我们常见的迷宫问题,本期我们通过前面学习过的数据结构---栈来去完美的解决这个问题,下面看问题!

    迷宫问题

            给定一个迷宫,指明起点和终点,找出从起点出发到终点的有效可行路径,就是迷宫问题(maze problem)。

    迷宫可以以二维数组来存储表示。0表示通路,1表示障碍。注意这里规定移动可以从上、下、左、右四方方向移动

    1. int maze[6][6] = {
    2. {1,1,1,1,1,1},
    3. {1,0,0,1,1,1},
    4. {1,0,0,0,0,0},
    5. {1,0,1,1,1,0},
    6. {1,0,0,0,0,1},
    7. {1,1,1,1,1,1}
    8. };

    算法思路

    1.栈的使用方法 

            迷宫问题,二维数组外围全是1的围墙,在里面规定一个起始地点和终点,这里我们要想找到起点到终点的路径,就需要一个数据结构去储存这个路径,这里可以用到栈的后进先出的特点,每次进入到一个可通过的点,就把这个点存入到栈当中,直到遇到了死胡同的时候,就回到上一个点,这时候就进行出栈的操作,然后走另外一条路,直到走到终点为止。

    2.方向的定义

    了解了栈的使用方法,那这里就要去定义移动的方向了,当走到某一个点的时候就要考虑优先往那边走,这时候可以根据迷宫的入口和出口大致方向去决定优先方向,这里的迷宫入口在出口的西北方向,那么优先的方向我依次为东、南、西、北,也就是说优先往东走,其次是南、西、北。方向的移动可以根据当前坐标进行上下左右的移动,只需要去定义一个方向数组,然后加上这个数组的方向即可。

     方向的储存结构:

    1. //试探方向存储结构
    2. typedef struct {
    3. int xx, yy;
    4. }Direction;
    5. //东南西北
    6. Direction dire[4] = { {0,1},{1,0},{0,-1},{-1,0} };

    代码实现

    栈的cpp代码:

    1. #include
    2. #include
    3. //数据类型
    4. typedef struct datatype {
    5. int x, y, di;
    6. }ElemType;
    7. //节点
    8. typedef struct node {
    9. ElemType data;
    10. struct node* next;
    11. }Node;
    12. //栈顶指示
    13. typedef struct stack {
    14. int count; //计数
    15. Node* point;
    16. }Stack;
    17. //创建节点
    18. Node* create_node(ElemType data) {
    19. Node* new_node = (Node*)malloc(sizeof(Node));
    20. if (new_node) {
    21. new_node->data = data;
    22. new_node->next = NULL;
    23. return new_node;
    24. }
    25. else
    26. {
    27. printf("ERROR\n");
    28. }
    29. }
    30. //初始化
    31. void stack_init(Stack* top) {
    32. top->count = 0;
    33. top->point = NULL;
    34. }
    35. int isEmpty(Stack* top) {
    36. if (top->count == 0) {
    37. return 1;
    38. }
    39. return 0;
    40. }
    41. //入栈
    42. void push(Stack* top, ElemType data) {
    43. Node* new_node = create_node(data);
    44. if (new_node) {
    45. top->count++;
    46. if (top->count == 1) {//如果入栈是第一个节点的话
    47. top->point = new_node;
    48. }
    49. else
    50. {
    51. new_node->next = top->point;
    52. top->point = new_node;
    53. }
    54. }
    55. else
    56. return;
    57. }
    58. //出栈
    59. Node* pop(Stack* top) {
    60. Node* pop_node = NULL;
    61. if (!isEmpty(top)) {
    62. pop_node = top->point;
    63. top->point = pop_node->next;
    64. top->count--;
    65. }
    66. return pop_node;
    67. }
    68. //递归输出路径
    69. void show_path(Node* node) {
    70. if (!node)
    71. return;
    72. show_path(node->next);
    73. printf("(%d,%d)\n", node->data.x, node->data.y);
    74. }

    栈的头文件h代码:

    1. #pragma once
    2. //链表栈
    3. //数据类型
    4. typedef struct datatype {
    5. int x, y, di;
    6. }ElemType;
    7. //节点
    8. typedef struct node {
    9. ElemType data;
    10. struct node* next;
    11. }Node;
    12. //栈顶指示
    13. typedef struct stack {
    14. int count; //计数
    15. Node* point;
    16. }Stack;
    17. void stack_init(Stack* top);
    18. int isEmpty(Stack* top);
    19. void push(Stack* top, ElemType data);
    20. Node* pop(Stack* top);
    21. void show_path(Node* node);

    走迷宫代码:

    1. #include
    2. #include
    3. #include"stack.h"
    4. //试探方向存储结构
    5. typedef struct {
    6. int xx, yy;
    7. }Direction;
    8. //东南西北
    9. Direction dire[4] = { {0,1},{1,0},{0,-1},{-1,0} };
    10. //判断能不能走出去,路径放入到了栈里面去
    11. bool Findpath(int maze[][6],Stack* stack ,Direction dir[],int startx,int starty,int endx,int endy) {
    12. //startx,starty是起点的坐标;endx、endy是终点的坐标.
    13. assert(stack);
    14. int x, y, di;
    15. int line, col;
    16. //初始化
    17. maze[startx][starty] = -1;
    18. ElemType start = { startx,starty,-1 };
    19. push(stack, start);
    20. while (!isEmpty(stack)) {
    21. Node* po = pop(stack);
    22. ElemType temp = po->data;
    23. x = temp.x;
    24. y = temp.y;
    25. di = temp.di++;
    26. //使得栈储存了一条通路
    27. while (di < 4) {
    28. line = x + dire[di].xx;
    29. col = y + dire[di].yy;
    30. if (maze[line][col] == 0) {
    31. //储存上一个节点的位置,入栈
    32. temp = { x,y,di };
    33. push(stack, temp);
    34. x = line;
    35. y = col;
    36. maze[line][col] = -1;
    37. if (x == endx && y == endy) {
    38. //把终点的位置入栈
    39. temp = { x,y,-1 };
    40. push(stack, temp);
    41. return true;
    42. }
    43. else
    44. di = 0;
    45. }
    46. else
    47. di++;
    48. }
    49. }
    50. return false;
    51. }
    52. int main() {
    53. int maze[6][6] = {
    54. {1,1,1,1,1,1},
    55. {1,0,0,1,1,1},
    56. {1,0,0,0,0,0},
    57. {1,0,1,1,1,0},
    58. {1,0,0,0,0,1},
    59. {1,1,1,1,1,1}
    60. };
    61. Stack stack;
    62. stack_init(&stack);
    63. printf("能否出去:%d\n", Findpath(maze, &stack, dire, 1, 1, 4, 4));
    64. show_path(stack.point);//输出遍历的结果
    65. }

    输出结果:

    好了,以上就是本期的全部内容了,我们下次见咯!

    分享一张壁纸: 

  • 相关阅读:
    springboot自动配置原理以及手动实现配置类
    Linux命令(128)之vmstat
    WAF Bypass及案例实战
    c++ 函数指针
    Git & GitHub 入门篇
    Android EditText限制只能输入整数和小数,且数字总长度小于等于11位(可自定义),整数5位(可自定义),小数5位(可自定义)
    基于Keras_bert模型的Bert使用与字词预测
    企业现在开始准备应对2024技术趋势了
    如何遍历HashMap集合?
    javaEE初阶---Servlet
  • 原文地址:https://blog.csdn.net/m0_73633088/article/details/133500192