-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsearch2.cpp
More file actions
204 lines (183 loc) · 4.51 KB
/
Asearch2.cpp
File metadata and controls
204 lines (183 loc) · 4.51 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
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using std::abs;
using std::cout;
using std::ifstream;
using std::istringstream;
using std::sort;
using std::string;
using std::vector;
// Etat Cellules
enum class State
{
kEmpty,
kObstacle,
kClosed,
kPath,
kStart,
kFinish
};
// Déplacement possibles
const int delta[4][2]{{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
vector<State> ParseLine(string line)
{
// Analyse caractère d'une ligne
istringstream sline(line);
int n;
char c;
vector<State> row;
while (sline >> n >> c )
{
if (n == 0)
{
row.push_back(State::kEmpty);
}
else
{
row.push_back(State::kObstacle);
}
}
return row;
}
vector<vector<State>> ReadBoardFile(string path)
{
// Lit le fichier ligne par ligne
ifstream myfile(path);
vector<vector<State>> board{};
if (myfile)
{
string line;
while (getline(myfile, line))
{
vector<State> row = ParseLine(line);
board.push_back(row);
}
}
return board;
}
// Compare la f value de 2 cellule
// f = g + h
bool Compare(const vector<int> a, const vector<int> b)
{
int f1 = a[2] + a[3];
int f2 = b[2] + b[3];
return f1 > f2;
}
void CellSort(vector<vector<int>> *v)
{
sort(v->begin(), v->end(), Compare);
}
// Calcule la distance
int Heuristic(int x1, int y1, int x2, int y2)
{
return abs(x2 - x1) + abs(y2 - y1);
}
// vérifie si la cellule fait partie de la grille et est libre
bool CheckValidCell(int x, int y, vector<vector<State>> &grid)
{
bool on_grid_x = (x >= 0 && x < grid.size());
bool on_grid_y = (y >= 0 && y < grid[0].size());
if (on_grid_x && on_grid_y)
return grid[x][y] == State::kEmpty;
return false;
}
void AddToOpen(int x, int y, int g, int h, vector<vector<int>> &openlist, vector<vector<State>> &grid)
{
// Ajoute à la liste des vecteurs ouverts et le déclare comme fermée
openlist.push_back(vector<int>{x, y, g, h});
grid[x][y] = State::kClosed;
}
void ExpandNeighbors(const vector<int> ¤t, int goal[2], vector<vector<int>> &openlist, vector<vector<State>> &grid)
{
//Recherche le trajet optimal pour les cellules voisines
// On obtient les valeurs de la cellule actuelle.
int x = current[0];
int y = current[1];
// boucle sur les cellules potentielles
for (auto &i : delta)
{
int x2 = x + i[0];
int y2 = y + i[1];
int g = current[2];
// Vérifie que la cellule voisine n'est fermé
if (CheckValidCell(x2, y2, grid))
{
// Increment g value and add neighbor to open list.
g++;
int h = Heuristic(x2, y2, goal[0], goal[1]);
AddToOpen(x2, y2, g, h, openlist, grid);
}
}
}
vector<vector<State>> Search(vector<vector<State>> grid, int init[2], int goal[2])
{
//Recherche du trajet optimal
vector<vector<int>> open{};
// initialisation
int x = init[0];
int y = init[1];
int g = 0;
int h = Heuristic(x, y, goal[0], goal[1]);
AddToOpen(x, y, g, h, open, grid);
while (!open.empty())
{
CellSort(&open);
auto current = open.back();
open.pop_back();
x = current[0];
y = current[1];
grid[x][y] = State::kPath;
// Vérifie si le programme est arrivé à destination
if (x == goal[0] && y == goal[1])
{
grid[init[0]][init[1]] = State::kStart;
grid[goal[0]][goal[1]] = State::kFinish;
return grid;
}
else
{
ExpandNeighbors(current, goal, open, grid);
}
}
cout << "No path found!" << std::endl;
return std::vector<vector<State>>{};
}
string CellString(State cell)
{
switch (cell)
{
case State::kObstacle:
return "🚧 ";
case State::kPath:
return "🚗 ";
case State::kStart:
return "🚦 ";
case State::kFinish:
return "🏁 ";
default:
return "0 ";
}
}
void PrintBoard(const vector<vector<State>> board)
{
for (int i = 0; i < board.size(); i++)
{
for (int j = 0; j < board[i].size(); j++)
{
cout << CellString(board[i][j]);
}
cout << std::endl;
}
}
int main()
{
int init[2]{0, 0};
int goal[2]{4, 11};
auto board = ReadBoardFile("2.board");
auto solution = Search(board, init, goal);
PrintBoard(solution);
}