鱼C论坛

 找回密码
 立即注册
查看: 555|回复: 3

利用c语言实现用队列求解迷宫问题的最短路径

[复制链接]
发表于 2023-10-15 18:52:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
利用c语言实现用队列求解迷宫问题的最短路径
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-15 18:53:44 | 显示全部楼层
在C语言中,我们可以使用广度优先搜索(BFS)算法和队列数据结构来求解迷宫问题的最短路径。以下是一个简单的实现:

首先,我们需要定义一个队列和一个节点:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define MAXSIZE 100

  4. typedef struct {
  5.     int x;
  6.     int y;
  7.     int step;
  8. } Node;

  9. typedef struct {
  10.     Node data[MAXSIZE];
  11.     int front;
  12.     int rear;
  13. } Queue;

  14. Queue* initQueue() {
  15.     Queue* q = (Queue*)malloc(sizeof(Queue));
  16.     q->front = 0;
  17.     q->rear = 0;
  18.     return q;
  19. }

  20. void enqueue(Queue* q, Node node) {
  21.     if ((q->rear + 1) % MAXSIZE == q->front) {
  22.         return;
  23.     }
  24.     q->data[q->rear] = node;
  25.     q->rear = (q->rear + 1) % MAXSIZE;
  26. }

  27. Node dequeue(Queue* q) {
  28.     if (q->front == q->rear) {
  29.         exit(0);
  30.     }
  31.     Node node = q->data[q->front];
  32.     q->front = (q->front + 1) % MAXSIZE;
  33.     return node;
  34. }

  35. int isEmpty(Queue* q) {
  36.     return q->front == q->rear;
  37. }
复制代码


然后,我们可以使用广度优先搜索(BFS)算法来找到迷宫的最短路径:

  1. #define N 5
  2. #define M 5

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

  10. int visited[N][M] = {0};

  11. int direction[4][2] = {
  12.     {-1, 0}, // up
  13.     {1, 0},  // down
  14.     {0, -1}, // left
  15.     {0, 1}   // right
  16. };

  17. void BFS(int x, int y) {
  18.     Queue* q = initQueue();
  19.     Node start = {x, y, 0};
  20.     enqueue(q, start);
  21.     visited[x][y] = 1;

  22.     while (!isEmpty(q)) {
  23.         Node cur = dequeue(q);
  24.         if (cur.x == N - 1 && cur.y == M - 1) {
  25.             printf("The shortest path is %d steps.\
  26. ", cur.step);
  27.             return;
  28.         }

  29.         for (int i = 0; i < 4; i++) {
  30.             int newX = cur.x + direction[ i][0];
  31.             int newY = cur.y + direction[ i][1];

  32.             if (newX >= 0 && newX < N && newY >= 0 && newY < M && maze[newX][newY] == 0 && visited[newX][newY] == 0) {
  33.                 Node next = {newX, newY, cur.step + 1};
  34.                 enqueue(q, next);
  35.                 visited[newX][newY] = 1;
  36.             }
  37.         }
  38.     }

  39.     printf("No path found.\
  40. ");
  41. }

  42. int main() {
  43.     BFS(0, 0);
  44.     return 0;
  45. }
复制代码


在这个代码中,我们首先定义了一个迷宫和一个访问数组。然后,我们使用广度优先搜索(BFS)算法来找到迷宫的最短路径。我们从起点开始,然后探索每个方向,如果新的位置是可达的(即不是墙壁,并且没有被访问过),我们就将其添加到队列中,并更新步数和访问数组。当我们到达终点时,我们就找到了最短路径。如果队列为空,那么迷宫没有解决方案。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-15 18:54:19 | 显示全部楼层
这是一个经典的迷宫问题,可以使用广度优先搜索算法来解决。以下是使用队列实现的c语言代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. #define ROW 10
  4. #define COL 10

  5. int maze[ROW][COL] = {
  6.     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  7.     {0, 1, 0, 1, 1, 1, 0, 0, 0, 0},
  8.     {0, 1, 0, 0, 0, 1, 0, 0, 0, 0},
  9.     {0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
  10.     {0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
  11.     {0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
  12.     {0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
  13.     {0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
  14.     {0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
  15.     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
  16. }; // 迷宫地图

  17. int visited[ROW][COL] = {0}; // 标记是否已经访问过

  18. int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}}; // 上下左右四个方向

  19. typedef struct {
  20.     int x;
  21.     int y;
  22.     int step;
  23. } Node; // 队列中的节点

  24. typedef struct {
  25.     Node data[ROW * COL];
  26.     int front;
  27.     int rear;
  28. } Queue; // 队列

  29. Queue q;

  30. void initQueue() {
  31.     q.front = 0;
  32.     q.rear = 0;
  33. }

  34. void enqueue(Node node) {
  35.     q.data[q.rear++] = node;
  36. }

  37. Node dequeue() {
  38.     return q.data[q.front++];
  39. }

  40. int isEmpty() {
  41.     return q.front == q.rear;
  42. }

  43. int bfs(int startX, int startY, int endX, int endY) {
  44.     initQueue();
  45.     Node start = {startX, startY, 0};
  46.     visited[startX][startY] = 1;
  47.     enqueue(start);
  48.     while (!isEmpty()) {
  49.         Node cur = dequeue();
  50.         if (cur.x == endX && cur.y == endY) {
  51.             return cur.step;
  52.         }
  53.         for (int i = 0; i < 4; i++) {
  54.             int nx = cur.x + dir[ i][0];
  55.             int ny = cur.y + dir[ i][1];
  56.             if (nx >= 0 && nx < ROW && ny >= 0 && ny < COL && !visited[nx][ny] && maze[nx][ny] == 0) {
  57.                 visited[nx][ny] = 1;
  58.                 Node next = {nx, ny, cur.step + 1};
  59.                 enqueue(next);
  60.             }
  61.         }
  62.     }
  63.     return -1; // 无法到达终点
  64. }

  65. int main() {
  66.     int startX = 1, startY = 1, endX = 8, endY = 8;
  67.     int steps = bfs(startX, startY, endX, endY);
  68.     printf("最短路径长度为:%d\n", steps);
  69.     return 0;
  70. }
复制代码


在上面的代码中,我们使用了一个队列来存储待访问的节点。首先将起点加入队列中,然后不断从队列中取出一个节点,遍历其四个相邻节点,如果相邻节点未被访问过且可通行,则加入队列中。当取出的节点为终点时,返回其步数即为最短路径长度。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-15 18:56:31 | 显示全部楼层
求解迷宫问题的最短路径需要使用广度优先搜索算法和队列数据结构来实现。下面是用C语言实现的代码示例:
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #define MAX_ROW 10
  4. #define MAX_COL 10
  5. // 迷宫地图的大小
  6. int row, col;
  7. // 迷宫地图
  8. int maze[MAX_ROW][MAX_COL] = {
  9.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  10.     {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
  11.     {1, 0, 0, 1, 0, 0, 0, 1, 0, 1},
  12.     {1, 0, 0, 0, 0, 1, 1, 0, 0, 1},
  13.     {1, 0, 1, 1, 1, 0, 0, 0, 0, 1},
  14.     {1, 0, 0, 0, 1, 0, 0, 1, 0, 1},
  15.     {1, 0, 1, 0, 0, 0, 0, 1, 0, 1},
  16.     {1, 0, 1, 1, 1, 0, 0, 1, 0, 1},
  17.     {1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
  18.     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  19. };
  20. // 定义坐标结构体
  21. typedef struct {
  22.     int x;
  23.     int y;
  24. } Coordinate;
  25. // 定义队列结构体
  26. typedef struct {
  27.     Coordinate data[MAX_ROW * MAX_COL];
  28.     int front;
  29.     int rear;
  30. } Queue;
  31. // 初始化队列
  32. void initQueue(Queue *q) {
  33.     q->front = 0;
  34.     q->rear = 0;
  35. }
  36. // 判断队列是否为空
  37. bool isEmpty(Queue *q) {
  38.     return q->front == q->rear;
  39. }
  40. // 入队
  41. void enqueue(Queue *q, Coordinate elem) {
  42.     q->data[q->rear] = elem;
  43.     q->rear++;
  44. }
  45. // 出队
  46. Coordinate dequeue(Queue *q) {
  47.     Coordinate elem = q->data[q->front];
  48.     q->front++;
  49.     return elem;
  50. }
  51. // 广度优先搜索求解迷宫最短路径
  52. bool breadthFirstSearch(Coordinate start, Coordinate end) {
  53.     Queue queue;
  54.     bool visited[MAX_ROW][MAX_COL] = {false}; // 记录每个位置是否已经访问过
  55.     initQueue(&queue);
  56.     enqueue(&queue, start);
  57.     visited[start.x][start.y] = true;
  58.     int offsetX[] = {-1, 1, 0, 0}; // 上下左右的偏移量
  59.     int offsetY[] = {0, 0, -1, 1};
  60.     while (!isEmpty(&queue)) {
  61.         Coordinate current = dequeue(&queue);
  62.         if (current.x == end.x && current.y == end.y) {
  63.             return true; // 找到了终点
  64.         }
  65.         // 遍历上下左右四个方向
  66.         for (int i = 0; i < 4; i++) {
  67.             int nextX = current.x + offsetX[i];
  68.             int nextY = current.y + offsetY[i];
  69.             // 判断是否可以移动到nextX, nextY,并且没有访问过
  70.             if (nextX >= 0 && nextX < row && nextY >= 0 && nextY < col &&
  71.                 maze[nextX][nextY] == 0 && !visited[nextX][nextY]) {
  72.                 Coordinate next = {nextX, nextY};
  73.                 enqueue(&queue, next);
  74.                 visited[nextX][nextY] = true;
  75.             }
  76.         }
  77.     }
  78.     return false; // 没有找到终点
  79. }
  80. int main() {
  81.     row = 10;
  82.     col = 10;
  83.     Coordinate start = {1, 1};
  84.     Coordinate end = {8, 8};
  85.     bool found = breadthFirstSearch(start, end);
  86.     if (found) {
  87.         printf("找到了最短路径!\n");
  88.     } else {
  89.         printf("无法找到最短路径!\n");
  90.     }
  91.     return 0;
  92. }
复制代码

以上代码中,使用 Coordinate 结构体表示迷宫中的坐标,Queue 结构体表示队列。initQueue 函数用于初始化队列,isEmpty 函数用于判断队列是否为空,enqueue 函数用于入队,dequeue 函数用于出队。

breadthFirstSearch 函数使用广度优先搜索算法来求解迷宫的最短路径。首先初始化队列,并将起始坐标入队,并标记为已访问。然后进行循环,直到队列为空。在每一步循环中,出队一个坐标,判断是否为终点坐标,如果是则返回 true 表示找到了最短路径。否则,遍历当前位置上下左右四个方向,判断是否可以移动到相邻位置,并且没有访问过,如果满足条件,则将相邻位置入队,并标记为已访问。循环结束后,如果没有找到终点,则返回 false。

最后,在 main 函数中定义迷宫的大小、起点和终点,调用 breadthFirstSearch 函数进行求解,并输出结果。

希望这样的回答能帮助到你,如果还有其他问题,请继续提问。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 04:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表