-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
executable file
·178 lines (155 loc) · 4.45 KB
/
main2.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
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
#define height 8
#define width 20
char maze[height][width];
int start_x, start_y, final_x, final_y;
struct block{
int f_cost;
pair <int, int> position;
pair<int, int> parent;
block(int f_c, pair<int, int> posn, pair<int, int> prt){
f_cost = f_c;
position = posn;
parent = prt;
}
};
vector <block> open;
vector <block> closed;
vector <block> neighbours;
block* find(block &B, vector <block> &A){
for(auto &i: A){
if(B.position == i.position){
return &i;
}
}
return nullptr;
}
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();
}
bool can_go(int i, int j){
return (maze[i][j] == ' ' || maze[i][j] == 'e') ? true : false;
}
int dist(int i, int j, int x, int y){
int base = abs(y - j);
int hgt = abs(x - i);
float distance = sqrt((base * base) + (hgt * hgt));
int h = (distance * 10);
return distance;
}
void get_neighbours(block A){
neighbours.clear();
for(int i = A.position.first - 1; i <= A.position.first + 1; i++){
for(int j = A.position.second - 1; j <= A.position.second + 1; j++){
if(can_go(i, j) && !(i == A.position.first && j == A.position.second)){
int f_c = dist(i, j,A.parent.first, A.parent.second) + dist(i, j, final_x, final_y);
pair <int, int> pos = make_pair(i, j);
pair <int, int> prt = A.position;
neighbours.push_back(block(f_c, pos, prt));
}
}
}
}
void print_maze(){
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
cout << maze[i][j];
}
cout << endl;
}
}
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;
}
bool compare_fc(block A, block B){
return (A.f_cost > B.f_cost) ? 0 : 1;
}
void find_path(block final){
block current = open.at(0);
cout << current.position.first <<" "<<current.position.second <<endl;
open.erase(open.begin());
closed.push_back(current);
if(current.position == final.position){
return;
}
get_neighbours(current);
for(auto neighbour: neighbours){
block* found_open = find(neighbour, open);
block* found_closed = find(neighbour, closed);
//neighbour found in open
if(found_open != nullptr){
if(found_open -> f_cost > neighbour.f_cost){
found_open -> f_cost = neighbour.f_cost;
found_open -> position = neighbour.position;
found_open -> parent = current.position;
}
}
//neighbour not found in closed and open
else if(found_closed == nullptr){
open.push_back(neighbour);
}
}
sort(open.begin(), open.end(), compare_fc);
//cout << open.at(0).position.first <<" "<<open.at(0).position.second <<endl;
find_path(final);
}
pair <int, int> parent(pair <int, int> posn){
for(auto i: closed){
if(i.position == posn){
return i.parent;
}
}
}
void add_path(){
pair <int, int> current = closed.at(closed.size() - 2).position;
while(current != make_pair(start_x, start_y)){
maze[current.first][current.second] = '*';
current = parent(current);
}
}
int main(){
get_maze();
open.clear();
closed.clear();
neighbours.clear();
int* ptr = get_points();
start_x = *ptr; ptr++;
start_y = *ptr; ptr++;
final_x = *ptr; ptr++;
final_y = *ptr;
pair <int, int> S = make_pair(start_x, start_y);
pair <int, int> F = make_pair(final_x, final_y);
block start(0, S, S);
block final(0, F, F);
open.push_back(start);
find_path(final);
add_path();
print_maze();
}