用c语言写一个代码:参考课本迷宫问题的求解,用栈求解迷宫问题的所有路径以及最短路径。

时间: 2023-11-21 19:02:25 浏览: 30
以下是用栈求解迷宫问题的所有路径以及最短路径的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #define MAX_ROW 10 #define MAX_COL 10 struct point { int row, col; } stack[512]; int top = 0; void push(struct point p) { stack[top++] = p; } struct point pop(void) { return stack[--top]; } int is_empty(void) { return top == 0; } int maze[MAX_ROW][MAX_COL] = { {0, 1, 0, 0, 0, 1, 0, 1, 0, 0}, {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}, {0, 0, 0, 1, 1, 1, 1, 1, 0, 1}, {0, 1, 0, 0, 0, 1, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 1, 1, 1, 1, 0}, {0, 1, 0, 1, 0, 0, 0, 0, 1, 0}, {0, 1, 0, 1, 1, 1, 1, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, }; void print_maze(void) { int i, j; for (i = 0; i < MAX_ROW; i++) { for (j = 0; j < MAX_COL; j++) printf("%d ", maze[i][j]); putchar('\n'); } printf("*********\n"); } void visit(int row, int col) { struct point visit_point = { row, col }; maze[row][col] = 2; push(visit_point); } int maze_path(int row, int col, int end_row, int end_col) { struct point visit_point = { row, col }; maze[row][col] = 2; push(visit_point); if (row == end_row && col == end_col) { printf("(%d, %d)\n", row, col); return 1; } if (row > 0 && maze[row-1][col] == 0) if (maze_path(row-1, col, end_row, end_col)) { printf("(%d, %d)\n", row, col); return 1; } if (row < MAX_ROW-1 && maze[row+1][col] == 0) if (maze_path(row+1, col, end_row, end_col)) { printf("(%d, %d)\n", row, col); return 1; } if (col > 0 && maze[row][col-1] == 0) if (maze_path(row, col-1, end_row, end_col)) { printf("(%d, %d)\n", row, col); return 1; } if (col < MAX_COL-1 && maze[row][col+1] == 0) if (maze_path(row, col+1, end_row, end_col)) { printf("(%d, %d)\n", row, col); return 1; } pop(); return 0; } void maze_shortest_path(int row, int col, int end_row, int end_col) { struct point visit_point = { row, col }; maze[row][col] = 2; push(visit_point); while (!is_empty()) { struct point cur = pop(); if (cur.row == end_row && cur.col == end_col) { struct point p; printf("(%d, %d)\n", cur.row, cur.col); while (!is_empty()) { p = pop(); printf("(%d, %d)\n", p.row, p.col); } return; } if (cur.row > 0 && maze[cur.row-1][cur.col] == 0) { visit(cur.row-1, cur.col); } if (cur.row < MAX_ROW-1 && maze[cur.row+1][cur.col] == 0) { visit(cur.row+1, cur.col); } if (cur.col > 0 && maze[cur.row][cur.col-1] == 0) { visit(cur.row, cur.col-1); } if (cur.col < MAX_COL-1 && maze[cur.row][cur.col+1] == 0) { visit(cur.row, cur.col+1); } } printf("No path found.\n"); } int main(void) { print_maze(); printf("The path is:\n"); maze_path(0, 0, 9, 9); printf("*********\n"); printf("The shortest path is:\n"); maze_shortest_path(0, 0, 9, 9); return 0; } ``` 该代码中,栈被用来记录路径上的点。maze数组表示迷宫,其中0表示可以通过的点,1表示障碍物,2表示已经访问过的点。迷宫的起点为(0, 0),终点为(9, 9)。maze_path函数用递归实现了深度优先搜索,寻找一条从起点到终点的路径,并打印出该路径。maze_shortest_path函数用广度优先搜索寻找起点到终点的最短路径,并打印出该路径。

相关推荐

最新推荐

recommend-type

C语言求解无向图顶点之间的所有最短路径

主要为大家详细介绍了C语言求解无向图顶点之间的所有最短路径,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言使用广度优先搜索算法解决迷宫问题(队列)

主要介绍了C语言使用广度优先搜索算法解决迷宫问题,结合迷宫问题分析了C语言队列广度优先搜索算法的相关使用技巧,需要的朋友可以参考下
recommend-type

C语言数据结构迷宫问题

 //往下一个坐标位置的方向}SElemType;typedef struct...{ SElemType *base; SElemType *top; int stacksize;}SqStack;//----------------- 栈的基本操作的算法实现 --------------------------------Status ...
recommend-type

C++用Dijkstra(迪杰斯特拉)算法求最短路径

Dijkstra(迪杰斯特拉)算法是典型的最短路径路由算法,用于计算一个节点到其他所有节点的最短路径。主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。下面这篇文章就给大家介绍关于C++用Dijkstra算法...
recommend-type

新建文本文档.txt

新建文本文档
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

HSV转为RGB的计算公式

HSV (Hue, Saturation, Value) 和 RGB (Red, Green, Blue) 是两种表示颜色的方式。下面是将 HSV 转换为 RGB 的计算公式: 1. 将 HSV 中的 S 和 V 值除以 100,得到范围在 0~1 之间的值。 2. 计算色相 H 在 RGB 中的值。如果 H 的范围在 0~60 或者 300~360 之间,则 R = V,G = (H/60)×V,B = 0。如果 H 的范围在 60~120 之间,则 R = ((120-H)/60)×V,G = V,B = 0。如果 H 的范围在 120~180 之间,则 R = 0,G = V,B =
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。