-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.cpp
More file actions
333 lines (277 loc) · 9.15 KB
/
Copy pathmap.cpp
File metadata and controls
333 lines (277 loc) · 9.15 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "map.h"
// --- Helper methods
auto Map::findNode(const Vector2 &l_node) const
{
return std::find_if(m_nodes.begin(), m_nodes.end(),
[&l_node] (auto &vertex)
{
return vertex.pos == l_node;
});
}
auto Map::findNode(char l_char) const
{
return std::find_if(m_nodes.cbegin(), m_nodes.cend(),
[&l_char] (auto &vertex)
{
return vertex.name == l_char;
});
}
auto Map::findNodeOrThrow(char l_char) const
{
auto itr = findNode(l_char);
if (itr == m_nodes.end()) {
throw std::out_of_range("graph does not contain such node");
}
return itr;
}
bool Map::isOutOfBounds(const CharMap &l_char_map, const Vector2 &l_node) const
{
if (l_node.x < 0 || l_node.x >= static_cast<int>(l_char_map.size())
|| l_node.y < 0 || l_node.y >= static_cast<int>(l_char_map.back().size()))
{
return true;
}
return false;
}
bool Map::isNode(const Vector2 &l_node) const
{
return findNode(l_node) != m_nodes.end();
}
bool Map::isEndpointChar(char l_ch) const
{
return l_ch >= 'A' && l_ch <= 'Z';
}
bool Map::loadCharMap(std::istream &stream)
{
std::string line;
while (std::getline(stream, line)) {
// This is to trim trailing \r character
line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
if (!m_char_map.size()) {
for (size_t i = 0; i < line.size(); ++i) {
m_char_map.push_back(DynArray<char>());
}
}
else if (line.size() != m_char_map.size()) {
return false;
}
for (size_t i = 0; i < m_char_map.size(); ++i) {
m_char_map.at(i).push_front(line.at(i));
}
}
return true;
}
void Map::loadEndpoints(const CharMap &l_char_map)
{
for (size_t i = 0; i < l_char_map.back().size(); ++i) {
for (size_t j = 0; j < l_char_map.size(); ++j) {
if (l_char_map.at(j).at(i) == m_endp_char) {
m_nodes.push_back(Vertex(static_cast<char>('A' + m_nodes.size()),
{static_cast<int>(j), static_cast<int>(i)}));
}
}
}
}
void Map::labelCharMap()
{
for (auto &node : m_nodes) {
if (node.name == '\0') {
break;
}
m_char_map
.at(static_cast<size_t>(node.pos.x))
.at(static_cast<size_t>(node.pos.y))
= node.name;
}
}
DynArray<Vector2> Map::initDirections()
{
DynArray<Vector2> directions(4);
directions[0] = Vector2(0, 1);
directions[1] = Vector2(1, 0);
directions[2] = Vector2(0, -1);
directions[3] = Vector2(-1, 0);
return directions;
}
void Map::initBFSQueue(const DynArray<Vector2> &l_directions,
const CharMap &l_char_map,
Queue<PosDir> &l_bfs_queue)
{
for (auto &dir : l_directions) {
auto &first_node_pos = m_nodes.at(0).pos;
if (l_char_map
.at(static_cast<size_t>(first_node_pos.x + dir.x))
.at(static_cast<size_t>(first_node_pos.y + dir.y))
== m_road_char)
{
l_bfs_queue.push({m_nodes.at(0).pos, dir});
}
}
}
void Map::checkAdjacentRoad(DynArray<Vector2> &l_road_dirs, const PosDir &l_pos_dir,
const DynArray<Vector2> &l_directions,
const CharMap &l_char_map, Vector2 &l_runner)
{
for (const auto &dir : l_directions) {
Vector2 check_road = l_runner + dir;
char check_char = l_char_map
.at(static_cast<size_t>(check_road.x))
.at(static_cast<size_t>(check_road.y));
if (isOutOfBounds(l_char_map, check_road)
|| check_road == l_runner - l_pos_dir.second)
{
continue;
}
if (check_char == m_mark_char && !isNode(check_road)) {
continue;
}
if (check_char == m_road_char
|| check_char == m_mark_char
|| isEndpointChar(check_char))
{
l_road_dirs.push_back(dir);
}
}
}
void Map::moveRunner(Vector2 &l_runner, const PosDir &l_pos_dir, Queue<PosDir> &l_bfs_queue,
CharMap &l_char_map, Roads &l_roads, const DynArray<Vector2> &l_directions)
{
while (true) {
DynArray<Vector2> road_dirs;
checkAdjacentRoad(road_dirs, l_pos_dir, l_directions, l_char_map, l_runner);
char &runner_char = l_char_map
.at(static_cast<size_t>(l_runner.x))
.at(static_cast<size_t>(l_runner.y));
if (!isEndpointChar(runner_char)) {
runner_char = m_mark_char;
}
if (road_dirs.size() == 1
&& road_dirs.back() == l_pos_dir.second
&& !isNode(l_runner))
{
l_runner += l_pos_dir.second;
}
else {
for (const auto &dir : road_dirs) {
l_bfs_queue.push({l_runner, dir});
}
if (!isNode(l_runner)) {
m_nodes.push_back(Vertex(l_runner));
}
l_roads.back().second = l_runner;
break;
}
}
}
void Map::handleBFSQueue(Queue<PosDir> &l_bfs_queue, CharMap &l_char_map,
Roads &l_roads, const DynArray<Vector2> &l_directions)
{
while (!l_bfs_queue.empty()) {
auto pos_dir = l_bfs_queue.top();
l_bfs_queue.pop();
Vector2 runner = pos_dir.first + pos_dir.second;
l_roads.push_back({pos_dir.first, Vector2(0, 0)});
moveRunner(runner, pos_dir, l_bfs_queue, l_char_map, l_roads, l_directions);
}
}
Roads Map::loadRoads(const CharMap &l_char_map)
{
CharMap ch_map_copy = l_char_map;
DynArray<Vector2> directions = initDirections();
loadEndpoints(ch_map_copy);
labelCharMap();
Roads roads;
Queue<PosDir> bfs_queue;
DynArray<Vector2> visited_nodes;
initBFSQueue(directions, ch_map_copy, bfs_queue);
handleBFSQueue(bfs_queue, ch_map_copy, roads, directions);
return roads;
}
auto Map::insertNode(const Vector2 &l_node)
{
auto itr = findNode(l_node);
if (itr == m_nodes.end()) {
m_nodes.push_back({l_node});
}
return itr;
}
void Map::loadGraph(Roads &l_roads)
{
for (auto &road : l_roads) {
auto first_itr = insertNode(road.first);
auto second_itr = insertNode(road.second);
Vector2 diff_vec = road.first - road.second;
auto v1_index = static_cast<size_t>(std::distance(m_nodes.cbegin(), first_itr));
auto v2_index = static_cast<size_t>(std::distance(m_nodes.cbegin(), second_itr));
auto distance = std::abs(diff_vec.x == 0 ? diff_vec.y : diff_vec.x);
m_graph.insertEdge(v1_index, v2_index, distance);
}
m_graph.finalize();
}
// --- Member functions
const CharMap &Map::getCharMap() const
{
return m_char_map;
}
void Map::markCorrectRoad(int l_start, int l_finish, int l_x_y, bool l_is_x,
CharMap &l_char_map) const
{
size_t i_begin = static_cast<size_t>(std::min(l_start, l_finish));
size_t i_end = static_cast<size_t>(std::max(l_start, l_finish));
for (size_t i = i_begin; i <= i_end; ++i) {
char *ch = nullptr;
if (l_is_x) {
ch = &l_char_map.at(static_cast<size_t>(l_x_y)).at(i);
}
else {
ch = &l_char_map.at(i).at(static_cast<size_t>(l_x_y));
}
if (*ch == m_road_char) {
*ch = m_correct_road;
}
}
}
CharMap Map::getCharMapWithPath(char l_vert1, char l_vert2) const
{
auto v1_itr = findNodeOrThrow(l_vert1);
auto v2_itr = findNodeOrThrow(l_vert2);
int v1_index = std::distance(m_nodes.begin(), v1_itr);
int v2_index = std::distance(m_nodes.begin(), v2_itr);
auto path = m_graph.getShortestPath(static_cast<size_t>(v1_index),
static_cast<size_t>(v2_index));
CharMap ch_map_copy = m_char_map;
Vector2 first_pos = m_nodes.at(static_cast<size_t>(path.top())).pos;
path.pop();
Vector2 second_pos;
while (!path.empty()) {
second_pos = m_nodes.at(static_cast<size_t>(path.top())).pos;
path.pop();
if (first_pos.x - second_pos.x == 0) {
markCorrectRoad(first_pos.y, second_pos.y, first_pos.x, true, ch_map_copy);
}
else if (first_pos.y - second_pos.y == 0) {
markCorrectRoad(first_pos.x, second_pos.x, first_pos.y, false, ch_map_copy);
}
first_pos = second_pos;
}
return ch_map_copy;
}
void Map::setCharMapEncoding(char l_road_char, char l_correct_char, char l_endp_char)
{
m_road_char = l_road_char;
m_correct_road = l_correct_char;
m_endp_char = l_endp_char;
}
bool Map::load(std::istream &stream)
{
if (m_road_char == '\0' || m_correct_road == '\0' || m_endp_char == '\0') {
return false;
}
if (!loadCharMap(stream)) {
return false;
}
auto roads = loadRoads(m_char_map);
loadGraph(roads);
return true;
}