数据结构建立校园导航系统代码
时间: 2023-07-06 21:41:39 浏览: 104
以下是使用C++语言和STL库实现校园导航系统的示例代码:
```cpp
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
using namespace std;
// 定义地图节点
struct MapNode {
int id;
string name;
vector<int> neighbors;
MapNode(int _id, string _name) : id(_id), name(_name) {}
};
// 定义校园地图
class CampusMap {
private:
vector<MapNode> mapNodes;
unordered_map<string, int> nameToId;
public:
// 添加地点到地图中
void addNode(int id, string name) {
mapNodes.emplace_back(id, name);
nameToId[name] = id;
}
// 添加路径到地图中
void addPath(int id1, int id2) {
mapNodes[id1].neighbors.push_back(id2);
mapNodes[id2].neighbors.push_back(id1);
}
// 根据名称查找地点
MapNode findNode(string name) {
return mapNodes[nameToId[name]];
}
// 根据起点和终点计算最短路径
vector<MapNode> shortestPath(string startName, string endName) {
int startId = nameToId[startName];
int endId = nameToId[endName];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> dist(mapNodes.size(), INT_MAX);
vector<int> prev(mapNodes.size(), -1);
pq.push(make_pair(0, startId));
dist[startId] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
if (u == endId) {
break;
}
for (auto v : mapNodes[u].neighbors) {
int w = 1; // 假设所有路径长度为1
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
prev[v] = u;
pq.push(make_pair(dist[v], v));
}
}
}
vector<MapNode> path;
for (int u = endId; u != -1; u = prev[u]) {
path.push_back(mapNodes[u]);
}
reverse(path.begin(), path.end());
return path;
}
};
int main() {
// 创建校园地图
CampusMap map;
map.addNode(0, "图书馆");
map.addNode(1, "教学楼A");
map.addNode(2, "教学楼B");
map.addNode(3, "实验楼");
map.addNode(4, "食堂");
map.addNode(5, "体育馆");
map.addNode(6, "行政楼");
map.addPath(0, 1);
map.addPath(0, 2);
map.addPath(1, 3);
map.addPath(1, 4);
map.addPath(2, 3);
map.addPath(2, 4);
map.addPath(3, 5);
map.addPath(4, 5);
map.addPath(6, 1);
// 计算最短路径
vector<MapNode> path = map.shortestPath("图书馆", "体育馆");
cout << "最短路径为:";
for (auto node : path) {
cout << node.name << " -> ";
}
cout << endl;
return 0;
}
```
在这个示例中,我们使用了vector、unordered_map、priority_queue等STL库中的数据结构来实现校园导航系统。首先,我们创建了一个CampusMap类,并定义了MapNode结构体来表示地图节点。然后,我们实现了添加地点和路径、查找地点、计算最短路径等功能。最后,我们在main函数中创建了一个校园地图,并计算了最短路径。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)