c语言用bfs算法求解迷宫,并显示出最短路径,若最短路径有很多,需要都显示出来
时间: 2024-01-28 10:03:36 浏览: 77
好的,以下是基于BFS算法求解迷宫并显示所有最短路径的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROW 100
#define MAX_COL 100
#define MAX_QUEUE_SIZE 100000
typedef struct {
int row;
int col;
} Point;
Point queue[MAX_QUEUE_SIZE];
int visited[MAX_ROW][MAX_COL];
int parent[MAX_ROW][MAX_COL];
int maze[MAX_ROW][MAX_COL];
int rows, cols, start_row, start_col, end_row, end_col;
int front = -1, rear = -1;
void enqueue(Point p)
{
if (rear == MAX_QUEUE_SIZE - 1)
{
printf("Queue overflow!\n");
exit(1);
}
queue[++rear] = p;
}
Point dequeue()
{
if (front == rear)
{
printf("Queue underflow!\n");
exit(1);
}
return queue[++front];
}
int is_queue_empty()
{
return front == rear;
}
void print_path(Point p)
{
if (parent[p.row][p.col] == -1)
{
printf("(%d,%d)", p.row, p.col);
return;
}
print_path((Point){parent[p.row][p.col] / cols, parent[p.row][p.col] % cols});
printf("->(%d,%d)", p.row, p.col);
}
void bfs()
{
Point current = {start_row, start_col};
enqueue(current);
visited[start_row][start_col] = 1;
parent[start_row][start_col] = -1;
while (!is_queue_empty())
{
current = dequeue();
if (current.row == end_row && current.col == end_col)
return;
if (current.row > 0 && maze[current.row - 1][current.col] == 0 && !visited[current.row - 1][current.col])
{
visited[current.row - 1][current.col] = 1;
parent[current.row - 1][current.col] = current.row * cols + current.col;
enqueue((Point){current.row - 1, current.col});
}
if (current.row < rows - 1 && maze[current.row + 1][current.col] == 0 && !visited[current.row + 1][current.col])
{
visited[current.row + 1][current.col] = 1;
parent[current.row + 1][current.col] = current.row * cols + current.col;
enqueue((Point){current.row + 1, current.col});
}
if (current.col > 0 && maze[current.row][current.col - 1] == 0 && !visited[current.row][current.col - 1])
{
visited[current.row][current.col - 1] = 1;
parent[current.row][current.col - 1] = current.row * cols + current.col;
enqueue((Point){current.row, current.col - 1});
}
if (current.col < cols - 1 && maze[current.row][current.col + 1] == 0 && !visited[current.row][current.col + 1])
{
visited[current.row][current.col + 1] = 1;
parent[current.row][current.col + 1] = current.row * cols + current.col;
enqueue((Point){current.row, current.col + 1});
}
}
}
int main()
{
printf("Please input the number of rows and cols of the maze:\n");
scanf("%d%d", &rows, &cols);
printf("Please input the maze, 0 for road, 1 for wall:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", &maze[i][j]);
printf("Please input the start point and the end point:\n");
scanf("%d%d%d%d", &start_row, &start_col, &end_row, &end_col);
bfs();
if (parent[end_row][end_col] == -1)
printf("No path found!\n");
else
{
printf("The shortest path is:\n");
print_path((Point){end_row, end_col});
printf("\n");
printf("All shortest paths are:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (parent[i][j] != -1 && parent[i][j] / cols == end_row && parent[i][j] % cols == end_col)
{
printf("(");
print_path((Point){i, j});
printf(")\n");
}
}
return 0;
}
```
该程序首先读入迷宫的大小和内容,然后读入起点和终点,并调用BFS算法求解最短路径。如果有多条最短路径,程序会把它们全部输出。
阅读全文