用c语言实现简单地图导航: 1.地图不低于五个点 2.可以在代码里设置好点名,路径,路径权重 3.程序运行,输入两个点,输出最短距离及最短路径
时间: 2024-02-24 08:54:11 浏览: 19
好的,这是一个比较有趣的问题,我可以帮你实现一个简单的地图导航程序。下面是一个示例代码,你可以根据自己的需求进行修改。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#define MAX_POINTS 5
#define MAX_NAME_LEN 20
// 点结构体
typedef struct {
char name[MAX_NAME_LEN];
int x;
int y;
} Point;
// 边结构体
typedef struct {
int from;
int to;
int weight;
} Edge;
// 地图结构体
typedef struct {
int num_points;
Point points[MAX_POINTS];
int num_edges;
Edge edges[MAX_POINTS * (MAX_POINTS - 1) / 2];
} Map;
// 初始化地图
void init_map(Map *map) {
// 设置点的名称和坐标
strcpy(map->points[0].name, "A");
map->points[0].x = 0;
map->points[0].y = 0;
strcpy(map->points[1].name, "B");
map->points[1].x = 1;
map->points[1].y = 1;
strcpy(map->points[2].name, "C");
map->points[2].x = 2;
map->points[2].y = 2;
strcpy(map->points[3].name, "D");
map->points[3].x = 3;
map->points[3].y = 3;
strcpy(map->points[4].name, "E");
map->points[4].x = 4;
map->points[4].y = 4;
// 设置边的权重
map->num_edges = 0;
for (int i = 0; i < map->num_points; i++) {
for (int j = i + 1; j < map->num_points; j++) {
map->edges[map->num_edges].from = i;
map->edges[map->num_edges].to = j;
int dx = map->points[i].x - map->points[j].x;
int dy = map->points[i].y - map->points[j].y;
map->edges[map->num_edges].weight = dx * dx + dy * dy; // 欧几里得距离的平方
map->num_edges++;
}
}
}
// 查找点的索引
int find_point(Map *map, char *name) {
for (int i = 0; i < map->num_points; i++) {
if (strcmp(map->points[i].name, name) == 0) {
return i;
}
}
return -1;
}
// 查找从from到to的边的索引
int find_edge(Map *map, int from, int to) {
for (int i = 0; i < map->num_edges; i++) {
if (map->edges[i].from == from && map->edges[i].to == to) {
return i;
}
if (map->edges[i].from == to && map->edges[i].to == from) {
return i;
}
}
return -1;
}
// Dijkstra算法
void shortest_path(Map *map, int start, int end, int *path, int *path_len, int *dist) {
int visited[MAX_POINTS] = {0};
int prev[MAX_POINTS];
for (int i = 0; i < map->num_points; i++) {
dist[i] = INT_MAX;
prev[i] = -1;
}
dist[start] = 0;
for (int i = 0; i < map->num_points; i++) {
int min_dist = INT_MAX;
int min_index = -1;
for (int j = 0; j < map->num_points; j++) {
if (!visited[j] && dist[j] < min_dist) {
min_dist = dist[j];
min_index = j;
}
}
if (min_index == -1) {
break;
}
visited[min_index] = 1;
for (int j = 0; j < map->num_points; j++) {
if (!visited[j]) {
int edge_index = find_edge(map, min_index, j);
if (edge_index != -1) {
int new_dist = dist[min_index] + map->edges[edge_index].weight;
if (new_dist < dist[j]) {
dist[j] = new_dist;
prev[j] = min_index;
}
}
}
}
}
*path_len = 0;
int index = end;
while (index != -1) {
path[*path_len] = index;
(*path_len)++;
index = prev[index];
}
for (int i = 0; i < *path_len / 2; i++) {
int temp = path[i];
path[i] = path[*path_len - i - 1];
path[*path_len - i - 1] = temp;
}
}
int main() {
Map map;
init_map(&map);
printf("请输入起点名称:");
char start_name[MAX_NAME_LEN];
scanf("%s", start_name);
int start = find_point(&map, start_name);
if (start == -1) {
printf("起点不存在!\n");
return 1;
}
printf("请输入终点名称:");
char end_name[MAX_NAME_LEN];
scanf("%s", end_name);
int end = find_point(&map, end_name);
if (end == -1) {
printf("终点不存在!\n");
return 1;
}
int path[MAX_POINTS];
int path_len;
int dist[MAX_POINTS];
shortest_path(&map, start, end, path, &path_len, dist);
printf("最短距离为:%d\n", dist[end]);
printf("最短路径为:");
for (int i = 0; i < path_len; i++) {
printf("%s", map.points[path[i]].name);
if (i != path_len - 1) {
printf("->");
}
}
printf("\n");
return 0;
}
```
这个程序实现了一个具有五个点的地图,你可以在 `init_map` 函数中设置点的名称和坐标,以及边的权重。程序使用了 Dijkstra 算法来找到最短路径和最短距离。你可以通过输入起点和终点的名称来测试程序的功能。