用数据结构写一个校园导航的代码
时间: 2023-08-04 19:23:54 浏览: 46
好的,我可以为你提供一个基于图论算法的校园导航代码,其中包括了根据节点之间的距离构建图、使用Dijkstra算法求最短路径等功能。代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX_NODE_COUNT 100 // 最大节点数
typedef struct {
int id; // 节点编号
char name[20]; // 节点名称
int x; // 节点横坐标
int y; // 节点纵坐标
} Node;
typedef struct {
int from; // 起点节点编号
int to; // 终点节点编号
int distance; // 距离
} Edge;
Node nodes[MAX_NODE_COUNT]; // 存储所有节点的数组
int nodeCount = 0; // 节点数量
Edge edges[MAX_NODE_COUNT * MAX_NODE_COUNT]; // 存储所有边的数组
int edgeCount = 0; // 边数量
void addNode(int id, char* name, int x, int y) {
if (nodeCount >= MAX_NODE_COUNT) {
printf("节点数量已达上限,无法添加新节点!\n");
return;
}
nodes[nodeCount].id = id;
strcpy(nodes[nodeCount].name, name);
nodes[nodeCount].x = x;
nodes[nodeCount].y = y;
nodeCount++;
}
void addEdge(int from, int to, int distance) {
if (edgeCount >= MAX_NODE_COUNT * MAX_NODE_COUNT) {
printf("边数量已达上限,无法添加新边!\n");
return;
}
edges[edgeCount].from = from;
edges[edgeCount].to = to;
edges[edgeCount].distance = distance;
edgeCount++;
}
void buildGraph() {
// 添加节点
addNode(1, "图书馆", 100, 200);
addNode(2, "教学楼", 300, 400);
addNode(3, "食堂", 500, 600);
addNode(4, "体育馆", 700, 800);
addNode(5, "校门", 900, 1000);
// 添加边
addEdge(1, 2, 200);
addEdge(1, 3, 300);
addEdge(2, 3, 100);
addEdge(2, 4, 400);
addEdge(3, 4, 200);
addEdge(3, 5, 500);
addEdge(4, 5, 300);
}
void printNode(Node node) {
printf("%d: %s (%d, %d)\n", node.id, node.name, node.x, node.y);
}
void printAllNodes() {
printf("所有节点信息:\n");
for (int i = 0; i < nodeCount; i++) {
printNode(nodes[i]);
}
}
void printEdge(Edge edge) {
printf("%d -> %d : %d\n", edge.from, edge.to, edge.distance);
}
void printAllEdges() {
printf("所有边信息:\n");
for (int i = 0; i < edgeCount; i++) {
printEdge(edges[i]);
}
}
void dijkstra(int start, int end) {
int visited[MAX_NODE_COUNT] = {0};
int distance[MAX_NODE_COUNT];
int path[MAX_NODE_COUNT];
for (int i = 0; i < nodeCount; i++) {
distance[i] = INT_MAX;
path[i] = -1;
}
distance[start - 1] = 0;
path[start - 1] = start - 1;
for (int i = 0; i < nodeCount; i++) {
int minDistance = INT_MAX;
int minIndex = -1;
for (int j = 0; j < nodeCount; j++) {
if (!visited[j] && distance[j] < minDistance) {
minDistance = distance[j];
minIndex = j;
}
}
if (minIndex == -1) {
break;
}
visited[minIndex] = 1;
for (int j = 0; j < edgeCount; j++) {
if (edges[j].from == minIndex + 1) {
int to = edges[j].to - 1;
int newDistance = distance[minIndex] + edges[j].distance;
if (newDistance < distance[to]) {
distance[to] = newDistance;
path[to] = minIndex;
}
}
}
}
if (path[end - 1] == -1) {
printf("不存在从节点%d到节点%d的路径!\n", start, end);
return;
}
int pathStack[MAX_NODE_COUNT];
int pathStackCount = 0;
int currentIndex = end - 1;
while (currentIndex != start - 1) {
pathStack[pathStackCount++] = currentIndex;
currentIndex = path[currentIndex];
}
pathStack[pathStackCount++] = start - 1;
printf("从节点%d到节点%d的最短路径为:\n", start, end);
while (pathStackCount > 0) {
int nodeIndex = pathStack[--pathStackCount];
printNode(nodes[nodeIndex]);
}
printf("路径长度为:%d\n", distance[end - 1]);
}
int main() {
buildGraph();
printAllNodes();
printAllEdges();
dijkstra(1, 5);
return 0;
}
```
这段代码实现了一个基于Dijkstra算法的校园导航,其中包括了根据节点之间的距离构建图、使用Dijkstra算法求最短路径等功能。你可以根据需要进行修改和扩展,比如添加节点的详细信息、支持根据节点名称进行搜索等。