c语言代码实现这个问题
时间: 2024-02-18 15:00:06 浏览: 13
好的,以下是C语言代码实现,同样采用深度优先搜索算法:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_ROW 100
#define MAX_COL 100
int maze[MAX_ROW][MAX_COL];
int visited[MAX_ROW][MAX_COL];
void dfs(int row, int col, int end_row, int end_col, int path[], int len, int *shortest_len, int shortest_path[]) {
if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL) { // 越界
return;
}
if (visited[row][col] || maze[row][col] == 1) { // 已访问或障碍
return;
}
if (row == end_row && col == end_col) { // 到达终点
if (len < *shortest_len) { // 更新最短路径
*shortest_len = len;
for (int i = 0; i < len; i++) {
shortest_path[i] = path[i];
}
}
return;
}
visited[row][col] = 1; // 标记已访问
path[len] = row * MAX_COL + col; // 保存当前节点
len++;
dfs(row, col + 1, end_row, end_col, path, len, shortest_len, shortest_path); // 向右探索
dfs(row + 1, col, end_row, end_col, path, len, shortest_len, shortest_path); // 向下探索
dfs(row, col - 1, end_row, end_col, path, len, shortest_len, shortest_path); // 向左探索
dfs(row - 1, col, end_row, end_col, path, len, shortest_len, shortest_path); // 向上探索
visited[row][col] = 0; // 回溯,取消标记
}
int main() {
int row, col, end_row, end_col;
printf("请输入迷宫的行数和列数:");
scanf("%d %d", &row, &col);
printf("请输入迷宫的内容(0表示通路,1表示障碍):\n");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
scanf("%d", &maze[i][j]);
}
}
printf("请输入起点的行数和列数:");
int start_row, start_col;
scanf("%d %d", &start_row, &start_col);
printf("请输入终点的行数和列数:");
scanf("%d %d", &end_row, &end_col);
int path[MAX_ROW * MAX_COL], shortest_path[MAX_ROW * MAX_COL];
int len = 0, shortest_len = MAX_ROW * MAX_COL;
dfs(start_row, start_col, end_row, end_col, path, len, &shortest_len, shortest_path);
if (shortest_len == MAX_ROW * MAX_COL) { // 没有通路
printf("没有通路!\n");
} else { // 输出最短路径
printf("所有路径:\n");
for (int i = 0; i < len; i++) {
printf("(%d, %d) ", path[i] / MAX_COL, path[i] % MAX_COL);
}
printf("\n最短路径:\n");
for (int i = 0; i < shortest_len; i++) {
printf("(%d, %d) ", shortest_path[i] / MAX_COL, shortest_path[i] % MAX_COL);
}
printf("\n");
}
return 0;
}
```
其中,MAX_ROW和MAX_COL分别表示迷宫的最大行数和列数,visited数组用于标记已访问的节点,dfs函数用于进行深度优先搜索。程序会先读入迷宫的行数、列数以及内容,然后读入起点和终点的坐标,最后输出所有路径和最短路径。如果没有通路,则输出“没有通路!”。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)