-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
170 lines (121 loc) · 3.15 KB
/
graph.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// Created by sasuke on 2020/12/17.
//
#ifndef NAVIGATIONSYSTEM_GRAPH_H
#define NAVIGATIONSYSTEM_GRAPH_H
#include <QColor>//各种颜色表示不同
#include <QMap> //表示颜色
#include <QPoint>//点的坐标
#include <QQueue>//最短路径
#include <QStack>//最短路径
#include <QString>
#include <QVector>
#include <fstream>
#include <ostream>
#include <istream>
#include <QDebug>
#include <utility>
#define DEV 1
enum Status {
OK,
ERR
};
/**
* @brief 用于图中的路径的起点和终点
*/
struct Pair {
int from, to;
Pair() : Pair(-1, -1) {}
Pair(const Pair &other) : from(other.from), to(other.to) {}//拷贝构造函数
Pair(int a, int b) : from(a), to(b) {}
bool operator==(const Pair &other) const {
return from == other.from && to == other.to;
}
bool operator!=(const Pair &other) const {
if (other.from == from && other.to == to) {
return false;
} else if (other.to == from && other.from == to) {
return false;
}
return true;
}
};
/**
* @brief 用于起点和终点在加上权重,最短路径时有用
*/
struct Road {
Pair m_pair;
int weight;
Road() : Road(Pair(-1, -1), -1) {}
Road(const Road &other)
: m_pair(other.m_pair), weight(other.weight) {}//拷贝构造函数
Road(const Pair& pair, int w) : m_pair(pair), weight(w) {}
Road(int a, int b, int c) : m_pair(a, b), weight(c) {}
bool operator==(const Road &other) const {
return (this->m_pair == other.m_pair || (this->m_pair.to == other.m_pair.from &&
this->m_pair.from == other.m_pair.to)) &&
this->weight == other.weight;
}
};
/**
* @brief 边 最终的权重 距离 顶点
*/
class Arc {
public:
Arc();
~Arc();
void setDistance(int num);
void setWeight(int num);
int getDistance() const;
int getWeight() const;
private:
int distance;//距离
int m_weight{};//最终的权重
#if DEV == 2
//实验性内容
int group;
int weather;
#endif
};
class Vertex {
public:
~Vertex() = default;
Vertex() = default;
Vertex(QString context, int x, int y) : info(std::move(context)), point(x, y) {}
void setPos(QPoint pos);
QPoint getPos() const;
QString info;
QPoint point;
};
/**
* @brief 邻接矩阵 邻接表 权重的计算方式 颜色
*/
class Graph {
public:
Graph();
~Graph();
void init_from_file(const QString &fileName = "pre.txt");
void updateGraph(int group);
int getVexNum() const;
//添加顶点
void addVex(const QString &info, int x, int y);
//删除顶点
Status delVex(const QString &info);
//加边 注意发送信号槽
Status addArc(int start, int end, int weight);
Status addArc(const Road &road);
//删除边
Status deleteArc(int start, int end);
Status deleteArc(const Pair &pair);
QVector<QString> &findShortestRoad(int from, int to, QVector<int> &path);
QVector<Road> edge;//有效顶点的集合 这里最好使用哈希表
QPoint getVertex(int index);
QString getInfo(int index);
void setPos(int index, int x, int y);
private:
QVector<QVector<Arc>> arc;//邻接矩阵
QVector<Vertex> vertexes; //顶点集合
int vexNum, arcNum; //顶点数量 边的数量
QVector<QString> ans;
};
#endif// NAVIGATIONSYSTEM_GRAPH_H