-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
157 lines (135 loc) · 2.9 KB
/
Graph.cpp
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
#include "Graph.h"
#include <vector>
#include <iostream>
#include "Vertex.h"
#include <cassert>
#include "olcPixelGameEngine/olcPixelGameEngine.h"
Graph::Graph(size_t width, size_t height)
: _width(width)
, _height(height)
, _vertices(_height, std::vector<Vertex>(_width))
{
// set positions
for(size_t row = 0; row < this->_height; ++row)
{
for(size_t col = 0; col < this->_width; ++col)
{
this->_vertices[row][col].pos = olc::vi2d(int32_t(col), int32_t(row) ); // i dont understand why the pos is swapped in its arguments
}
}
// connect for later traversal
for(size_t row = 1; row < this->_height; ++row)
{
for(size_t col = 0; col < this->_width; ++col)
{
this->_vertices[row][col].connectNorth(&_vertices[row-1][col ]);
}
}
for(size_t row = 0; row < this->_height; ++row)
{
for(size_t col = 1; col < this->_width; ++col)
{
this->_vertices[row][col-1].connectEast(&_vertices[row ][col]);
}
}
}
Graph::~Graph()
{
}
void Graph::initializeHamiltonian()
{
Vertex* v = &this->_vertices[0][0];
while(v != nullptr)
{
v->unblockEast();
v = v->east;
}
v = &this->_vertices[0][0];
while(v != nullptr)
{
v->unblockSouth();
v = v->south;
}
v = &this->_vertices[this->_height-1][0];
while(v != nullptr)
{
v->unblockEast();
v = v->east;
}
for(size_t row = 1; row < this->_height; ++row)
{
for(size_t col = 1; col < this->_width; ++col)
{
this->_vertices[row][col].unblockEast();
}
}
for(size_t row = 0; row < this->_height; ++row)
{
if(row % 2 == 1)
this->_vertices[row][1].unblockSouth();
else
this->_vertices[row][this->_width-1].unblockSouth();
}
}
void Graph::calculateDistances(Vertex* origin)
{
origin->distanceToApple = 0;
Vertex* n = origin->north;
Vertex* s = origin->south;
Vertex* e = origin->east;
Vertex* w = origin->west;
while(e != nullptr)
{
e->distanceToApple = e->west->distanceToApple +1;
e = e->east;
}
while(w != nullptr)
{
w->distanceToApple = w->east->distanceToApple +1;
w = w->west;
}
while(n != nullptr)
{
n->distanceToApple = n->south->distanceToApple +1;
Vertex* e = n->east;
Vertex* w = n->west;
while(e != nullptr)
{
e->distanceToApple = e->west->distanceToApple +1;
e = e->east;
}
while(w != nullptr)
{
w->distanceToApple = w->east->distanceToApple +1;
w = w->west;
}
n = n->north;
}
while(s != nullptr)
{
s->distanceToApple = s->north->distanceToApple +1;
Vertex* e = s->east;
Vertex* w = s->west;
while(e != nullptr)
{
e->distanceToApple = e->west->distanceToApple +1;
e = e->east;
}
while(w != nullptr)
{
w->distanceToApple = w->east->distanceToApple +1;
w = w->west;
}
s = s->south;
}
}
Vertex* Graph::getVertex(size_t row, size_t col)
{
assert(row < this->_vertices.size());
assert(col < this->_vertices[0].size());
return &this->_vertices[row][col];
}
Vertex* Graph::getVertex(const olc::vi2d& pos)
{
return this->getVertex(pos.y, pos.x);
}