用户自定义迷宫地图,指定入口和出口,采用图相关算法寻找一条出入口之间最短路径用C语言写
时间: 2024-01-17 08:05:03 浏览: 75
求迷宫的最短路径:现要求设计一个算法找一条从迷宫入口到出口的最短路径。
4星 · 用户满意度95%
以下是采用Dijkstra算法寻找最短路径的C语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 100
typedef struct {
int x; //横坐标
int y; //纵坐标
} Point;
typedef struct {
Point point; //点的坐标
int dist; //起点到该点的距离
bool visited; //该点是否已被访问
} Vertex;
Vertex graph[MAX_SIZE][MAX_SIZE]; //图
int rows, cols; //地图的行数和列数
//获取指定点的邻接点
void getNeighbors(Point point, Point neighbors[])
{
int i, j, cnt = 0;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue;
int x = point.x + i;
int y = point.y + j;
if (x >= 0 && x < rows && y >= 0 && y < cols) {
if (!graph[x][y].visited) {
neighbors[cnt++] = (Point){x, y};
}
}
}
}
}
//计算两点之间的距离
int getDistance(Point p1, Point p2)
{
int dx = p1.x - p2.x;
int dy = p1.y - p2.y;
return dx * dx + dy * dy;
}
//查找当前未访问的距离起点最近的点
Vertex* getMinVertex()
{
int i, j;
Vertex* minVertex = NULL;
int minDist = -1;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (!graph[i][j].visited && graph[i][j].dist >= 0) {
if (minDist < 0 || graph[i][j].dist < minDist) {
minDist = graph[i][j].dist;
minVertex = &graph[i][j];
}
}
}
}
return minVertex;
}
//使用Dijkstra算法寻找最短路径
void findShortestPath(Point start, Point end)
{
int i, j;
//初始化每个点的起点距离和是否已访问
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
graph[i][j].point = (Point){i, j};
graph[i][j].dist = -1;
graph[i][j].visited = false;
}
}
graph[start.x][start.y].dist = 0;
//循环查找最短路径
while (true) {
Vertex* currVertex = getMinVertex();
if (currVertex == NULL) break;
currVertex->visited = true;
if (currVertex->point.x == end.x && currVertex->point.y == end.y) break;
Point neighbors[8];
getNeighbors(currVertex->point, neighbors);
for (i = 0; i < 8; i++) {
if (neighbors[i].x >= 0 && neighbors[i].y >= 0) {
int dist = currVertex->dist + getDistance(currVertex->point, neighbors[i]);
if (graph[neighbors[i].x][neighbors[i].y].dist < 0 || dist < graph[neighbors[i].x][neighbors[i].y].dist) {
graph[neighbors[i].x][neighbors[i].y].dist = dist;
}
}
}
}
//输出最短路径和距离
printf("Shortest distance from (%d, %d) to (%d, %d) is %d\n", start.x, start.y, end.x, end.y, graph[end.x][end.y].dist);
printf("Shortest path: ");
Vertex* currVertex = &graph[end.x][end.y];
while (currVertex != NULL) {
printf("(%d, %d) ", currVertex->point.x, currVertex->point.y);
Point neighbors[8];
getNeighbors(currVertex->point, neighbors);
int minDist = -1;
Vertex* nextVertex = NULL;
for (i = 0; i < 8; i++) {
if (neighbors[i].x >= 0 && neighbors[i].y >= 0) {
if (graph[neighbors[i].x][neighbors[i].y].dist >= 0) {
int dist = getDistance(currVertex->point, neighbors[i]) + graph[neighbors[i].x][neighbors[i].y].dist;
if (minDist < 0 || dist < minDist) {
minDist = dist;
nextVertex = &graph[neighbors[i].x][neighbors[i].y];
}
}
}
}
currVertex = nextVertex;
}
}
int main()
{
int i, j;
//读入地图
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
printf("Enter the map:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
char c;
do {
c = getchar();
} while (c != '.' && c != 'S' && c != 'E');
if (c == 'S') {
graph[i][j].dist = 0;
graph[i][j].visited = true;
} else if (c == 'E') {
graph[i][j].visited = false;
} else {
graph[i][j].visited = false;
}
}
getchar(); //消耗行末换行符
}
//查找起点和终点
Point start, end;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if (graph[i][j].dist >= 0) {
start = (Point){i, j};
} else if (!graph[i][j].visited) {
end = (Point){i, j};
}
}
}
//查找最短路径
findShortestPath(start, end);
return 0;
}
```
该程序从标准输入读入地图,其中"."表示可通过的格子,"S"表示起点,"E"表示终点。输出起点到终点的最短距离和路径。
阅读全文