-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjikstra.cc
195 lines (181 loc) · 9.22 KB
/
djikstra.cc
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
#include <iostream>
#include "djikstra.hh"
namespace Djikstra
{
Djikstra::Djikstra(Map* map, Tile&& start, Tile&& end) : map_(map),
start_(start),
end_(end)
{
if (!start_.is_start())
start_.start_set();
if (!end_.is_arrival())
end_.arrival_set();
start_.listed_set();
}
Djikstra::Djikstra(Map* map, Tile& start, Tile& end) : map_(map),
start_(start),
end_(end)
{
if (!start_.is_start())
start_.start_set();
if (!end_.is_arrival())
end_.arrival_set();
start_.listed_set();
}
void display(Tile* c)
{
std::cout << "current : x : " << c->x_get() << " y : "<< c->y_get() << " w : " << c->weight_get() << "\n";
}
Tile* Djikstra::search()
{
std::vector<Tile*> closed;
std::vector<Tile*> open_;
Tile* current = nullptr;
Tile* neighbour = nullptr;
int weight = 0;
open_.push_back(&start_);
while (open_.size() > 0)
{
current = (*map_)(open_[0]->x_get(), open_[0]->y_get());
closed.push_back(current);
open_.erase(open_.begin());
/*Stop when we have found the end of the road*/
display(current); /* debugging*/
if (current->x_get() == end_.x_get() && current->y_get() == end_.y_get())
break;
else
{
/* We check all the neighbour around */
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
std::cout << "\n\n Map : \n";
for (int i = 0; i < map_->h_get(); i++)
{
for (int j = 0; j < map_->w_get(); j++)
{
std::cout << ((*map_)(i, j))->weight_get() << " ";
}
std::cout << "\n";
}
if (!((x == 0) && (y == 0)))
{
if (current->x_get() + x >= 0 && current->x_get() + x < map_->h_get()
&& current->y_get() + y >= 0 && current->y_get() + y < map_->w_get())
{
if ((*map_)(current->x_get() + x, current->y_get() + y)->type_get() ==
Tile::Type::WALL
/* && (*map_)(current->x_get(), current->y_get() + y)->type_get() ==
Tile::Type::WALL*/)
{
continue;
}
neighbour = (*map_)(current->x_get() + x, current->y_get() + y);
if ((x != 0) && (y != 0))
weight = 14;
else
weight = 10;
if (neighbour->type_get() != Tile::Type::WALL)
{
if (!neighbour->is_listed())
{
neighbour->listed_set(); // have been visited
neighbour->father_set(current);
neighbour->weight_set(current->weight_get() + weight); // new weight of the node
if (open_.size() > 0) // if there is Tile in the array
{
bool inserted = false;
for (unsigned int i = 0; i < open_.size() && !inserted; i++)
{
/*replace Tile at i by a Tile with a lower weight*/
if (neighbour->weight_get() < open_[i]->weight_get())
{
open_.insert(open_.begin() + i, neighbour);
inserted = true;
}
}
if (!inserted)
open_.push_back(neighbour);
}
else
open_.push_back(neighbour);
}
else
{
/* if this node have already been visited we will have is weight that is lower
than CurrWeight + weight */
if (current->weight_get() + weight < neighbour->weight_get())
{
bool inopen = false;
neighbour->weight_set(current->weight_get() + weight);
neighbour->father_set(current);
for (unsigned int i = 0; i < open_.size(); i++)
{
// delete the node at is old position
if (open_[i] == neighbour)
{
inopen = true;
open_.erase(open_.begin() + i);
bool inserted = false;
for (unsigned int j = 0; j < open_.size() && !inserted; j++)
{
if (neighbour->weight_get() < open_[j]->weight_get())
{
open_.insert(open_.begin() + j, neighbour);
inserted = true;
}
}
if (!inserted)
open_.push_back(neighbour);
}
}
/*
If the node is listed but is not in the open array that mean that
this point constitute the path so we erase this point of the path.
*/
if (!inopen)
{
for(unsigned int i = 0; i < closed.size(); i++)
{
if (neighbour == closed[i])
{
closed.erase(closed.begin() + i);
bool inserted = false;
for (unsigned int j = 0; j < open_.size() && !inserted; j++)
{
if (neighbour->weight_get() < open_[j]->weight_get())
{
open_.insert(open_.begin() + j, neighbour);
inserted = true;
}
}
if (!inserted)
open_.push_back(neighbour);
}
}
}
}
}
}
}
}
}
}
}
}
if (current == (*map_)(end_.x_get(), end_.y_get()))
{
do
{
path_.push_back(current);
current = current->father_get();
} while (current != (*map_)(start_.x_get(), start_.y_get()));
path_.push_back(current);
}
std::cout << "\n\n Road is : \n\n\n";
for (auto it : path_)
display(it);
return closed[0];
}
}