校园导航数据结构C语言
时间: 2023-12-04 09:41:19 浏览: 60
校园导航系统是一个基于数据结构的C语言程序,主要功能是实现校园地图的导航和最短路径的输出。该程序需要读者自己进行一些景点介绍和校园介绍的存储路径的更改,才能成功实现导航功能。该程序使用了图的数据结构,通过Dijkstra算法实现最短路径的计算。如果您想了解更多关于该程序的实现细节,可以参考引用中的文章。
以下是一个简单的Dijkstra算法的实现,用于计算从起点到终点的最短路径:
```c
#include <stdio.h>
#include <limits.h>
#define V 9
int minDistance(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
void printPath(int parent[], int j)
{
if (parent[j] == -1)
return;
printPath(parent, parent[j]);
printf("%d ", j);
}
void printSolution(int dist[], int n, int parent[], int src, int dest)
{
printf("Shortest path from %d to %d:\n", src, dest);
printf("Vertex Distance Path\n");
for (int i = 0; i < V; i++)
{
if (i == dest)
{
printf("%d \t\t %d\t\t%d ", i, dist[i], src);
printPath(parent, i);
}
}
}
void dijkstra(int graph[V][V], int src, int dest)
{
int dist[V];
bool sptSet[V];
int parent[V];
for (int i = 0; i < V; i++)
{
parent[src] = -1;
dist[i] = INT_MAX;
sptSet[i] = false;
}
dist[src] = 0;
for (int count = 0; count < V - 1; count++)
{
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
{
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
{
parent[v] = u;
dist[v] = dist[u] + graph[u][v];
}
}
}
printSolution(dist, V, parent, src, dest);
}
int main()
{
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}};
dijkstra(graph, 0, 8);
return 0;
}
```
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)