-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
193 lines (168 loc) · 5.04 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define WIDTH 50
#define HEIGHT 25
#define WALLS 5
void printArray(int array[HEIGHT][WIDTH]) {
for (int y=0; y<HEIGHT; y++) {
for (int x=0; x<WIDTH; x++) {
printf("%c", array[y][x] == 1 ? 'O' : array[y][x] == 2 ? '.' : ' ');
}
printf("\n");
}
}
void printCostArray(double array[HEIGHT][WIDTH]) {
for (int y=0; y<HEIGHT; y++) {
for (int x=0; x<WIDTH; x++) {
printf("%f ", array[y][x]);
}
printf("\n");
}
}
void initializeArray(int array[HEIGHT][WIDTH]) {
// Set random seed
srand(time(0));
// Initalize array
for (int y=0; y<HEIGHT; y++) {
for (int x=0; x<WIDTH; x++) {
int value = rand() % WALLS;
if (value != 1) value = 0;
array[y][x] = value;
}
}
// Add walls
for (int i=0; i<WIDTH; i++) {
array[0][i] = 1;
array[HEIGHT-1][i] = 1;
}
for (int i=0; i<HEIGHT; i++) {
array[i][0] = 1;
array[i][WIDTH-1] = 1;
}
}
void cleanIntArray(int array[HEIGHT][WIDTH]) {
for (int y=0; y<HEIGHT; y++) {
for (int x=0; x<WIDTH; x++) {
array[y][x] = 0;
}
}
}
void cleanDoubleArray(double array[HEIGHT][WIDTH]) {
for (int y=0; y<HEIGHT; y++) {
for (int x=0; x<WIDTH; x++) {
array[y][x] = 0;
}
}
}
void cleanParentArray(int array[HEIGHT][WIDTH][2]) {
for (int y=0; y<HEIGHT; y++) {
for (int x=0; x<WIDTH; x++) {
array[y][x][0] = -1;
array[y][x][1] = -1;
}
}
}
void calculateHeuristics(double h[HEIGHT][WIDTH], double g[HEIGHT][WIDTH], double f[HEIGHT][WIDTH], int end[2]) {
for (int y=0; y<HEIGHT; y++) {
for (int x=0; x<WIDTH; x++) {
h[y][x] = sqrt(pow(end[1] - x, 2) + pow(end[0] - y, 2));
f[y][x] = g[y][x] + h[y][x];
}
}
}
int* getNewCurrent(double f[HEIGHT][WIDTH], int array[HEIGHT][WIDTH], int open[HEIGHT][WIDTH]) {
static int lowest[2] = {-1, -1};
int lowestValue = 99999;
for (int y=0; y<HEIGHT; y++) {
for (int x=0; x<WIDTH; x++) {
if (f[y][x] < lowestValue && open[y][x] == 1) {
lowest[0] = y;
lowest[1] = x;
lowestValue = f[lowest[0]][lowest[1]];
}
}
}
return lowest;
}
void reconstruct(int* current, int parent[HEIGHT][WIDTH][2], int array[HEIGHT][WIDTH]) {
while (parent[current[0]][current[1]][0] != -1) {
array[parent[current[0]][current[1]][0]][parent[current[0]][current[1]][1]] = 2;
current[0] = parent[current[0]][current[1]][0];
current[1] = parent[current[0]][current[1]][1];
}
printf("\n");
printArray(array);
printf("\n");
}
int main(int argc, char** argv) {
int array[HEIGHT][WIDTH];
int parent[HEIGHT][WIDTH][2];
double f[HEIGHT][WIDTH];
double g[HEIGHT][WIDTH];
double h[HEIGHT][WIDTH];
int* current;
int done = 0;
int numOpen = 0;
int open[HEIGHT][WIDTH];
int closed[HEIGHT][WIDTH];
int start[2] = {1, 1};
int end[2] = {HEIGHT-2, WIDTH-2};
array[start[0]][start[1]] = 2;
array[end[0]][end[1]] = 2;
initializeArray(array);
cleanParentArray(parent);
cleanDoubleArray(f);
cleanDoubleArray(g);
cleanDoubleArray(h);
calculateHeuristics(h, g, f, end);
cleanIntArray(open);
cleanIntArray(closed);
open[start[0]][start[1]] = 1;
numOpen++;
while (numOpen > 0 && done == 0) {
current = getNewCurrent(f, array, open);
if (current[0] == end[0] && current[1] == end[1]) {
done = 1;
reconstruct(current, parent, array);
return 0;
}
open[current[0]][current[1]] = 0;
numOpen--;
closed[current[0]][current[1]] = 1;
for (int y=current[0]-1; y<current[0]+2; y++) {
for (int x=current[1]-1; x<current[1]+2; x++) {
if (x < 0 || y < 0 || x > WIDTH-1 || y > HEIGHT-1 ||
abs(current[0] - y) == abs(current[1] - x) ||
closed[y][x] == 1 || array[y][x] == 1) {
continue;
}
double tempG = g[current[0]][current[1]] + 1;
if (open[y][x] == 1) {
if (tempG < g[y][x]) {
g[y][x] = tempG;
f[y][x] = g[y][x] + h[y][x];
parent[y][x][0] = current[0];
parent[y][x][1] = current[1];
}
}
else {
g[y][x] = tempG;
open[y][x] = 1;
numOpen++;
f[y][x] = g[y][x] + h[y][x];
parent[y][x][0] = current[0];
parent[y][x][1] = current[1];
}
}
}
}
if (numOpen == 0 && done == 0) {
done = 1;
printf("\n");
printArray(array);
printf("\nNo solution!\n\n");
}
return 0;
}