-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_finder.cpp
executable file
·187 lines (168 loc) · 4.54 KB
/
path_finder.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
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
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define height 12
#define width 30
struct block{
pair <int, int> position;
int g_cost = 0;
int h_cost = 0;
block* parent = nullptr;
};
char maze[height][width];
int a = 0;
static block grid[height][width];
vector <block*> open;
vector <block*> closed;
vector <block*> neighbours;
block *start, *finish ;
bool in_open(block* A){
for(auto i: open){
if(i -> position == A -> position){
return true;
}
}
return false;
}
bool in_closed(block* A){
for(auto i: closed){
if(i -> position == A -> position){
return true;
}
}
return false;
}
void get_maze(){
fstream file;
file.open("maze.txt");
string line;
int line_no = 0;
while(getline(file, line)){
for(int j = 0; j < width; j++){
maze[line_no][j] = line[j];
}
line_no++;
}
file.close();
}
void reset_grid(){
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
grid[i][j].position = (make_pair(i, j));
}
}
}
bool can_go(pair <int, int> posn){
return (maze[posn.first][posn.second] == ' ' || maze[posn.first][posn.second] == 'e') ? true : false;
}
int dist(pair <int, int> A, pair <int, int> B){
int base = abs(B.second - A.second);
int hgt = abs(B.first - A.first);
float distance = sqrt((base * base) + (hgt * hgt));
int h = (distance * 10);
return h;
}
void get_neighbours(pair <int, int> position){
neighbours.clear();
for(int i = position.first - 1; i <= position.first + 1; i++){
for(int j = position.second - 1; j <= position.second + 1; j++){
pair <int, int> posn = make_pair(i, j);
if(can_go(posn) && !(i == position.first && j == position.second)){
neighbours.push_back(&grid[i][j]);
}
}
}
}
bool compare_fc(block *A, block *B){
return ((A -> g_cost + A -> h_cost) >= (B -> g_cost + B -> h_cost)) ? 0 : 1;
}
int* get_points(){
static int arr[4];
int* p = &arr[0];
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
if(maze[i][j] == 's'){
arr[0] = i;
arr[1] = j;
}else if(maze[i][j] == 'e'){
arr[2] = i;
arr[3] = j;
}else{
continue;
}
}
}
return p;
}
void find_path(){
sort(open.begin(), open.end(), compare_fc);
block* current = open.at(0);
if(current == finish){
return;
}
open.erase(open.begin());
closed.push_back(current);
get_neighbours(current -> position);
for(auto & neighbour: neighbours){
if(in_open(neighbour)){
if((current -> g_cost + dist(neighbour->position, current->position)) < neighbour -> g_cost){
neighbour -> g_cost = (current -> g_cost + dist(neighbour->position, current->position));
neighbour -> parent = current;
}
}else if(!(in_closed(neighbour) || in_open(neighbour))){
int g_c = (current -> g_cost + dist(neighbour->position, current->position));
int h_c = dist(neighbour -> position, finish -> position);
neighbour -> g_cost = g_c;
neighbour -> h_cost = h_c;
neighbour -> parent = current;
open.push_back(neighbour);
}
}
find_path();
}
pair <int, int> parent(pair <int, int> posn){
for(auto i: closed){
if(i -> position == posn){
return i -> parent->position;
}
}
return make_pair(0, 0);
}
void add_path(){
block* current = closed.at(closed.size() - 1);
while(current -> position != start -> position){
maze[current-> position.first][current->position.second] = '*';
current = current -> parent;
}
}
void print_maze(){
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
cout << maze[i][j];
}
cout << endl;
}
}
int main(){
reset_grid();
get_maze();
cout << "maze: " <<endl;
print_maze();
open.clear();
closed.clear();
neighbours.clear();
int* ptr = get_points();
int start_x = *ptr; ptr++;
int start_y = *ptr; ptr++;
int final_x = *ptr; ptr++;
int final_y = *ptr;
start = &grid[start_x][start_y];
finish = &grid[final_x][final_y];
start -> g_cost = 0;
start -> parent = nullptr;
open.push_back(start);
find_path();
cout << "path found: " <<endl;
add_path();
print_maze();
}