-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
363 lines (289 loc) · 13.9 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <stdio.h>
#include <stdlib.h>
// Neighbors linked list
typedef struct node {
int y;
int x;
int distance;
struct node* next;
} node;
node* CreateNode(int yIndex, int xIndex, int nodeDistance) {
node *result = malloc(sizeof(node));
result->y = yIndex;
result->x = xIndex;
result->distance = nodeDistance;
result->next = NULL;
return result;
}
// Heap queue
typedef struct queue {
int y;
int x;
int distance;
} queue;
queue* CreateQueue(int yIndex, int xIndex, int nodeDistance) {
queue* result = malloc(sizeof(queue));
result->y = yIndex;
result->x = xIndex;
result->distance = nodeDistance;
return result;
}
// Backtrack path
typedef struct path {
int y;
int x;
int shortestDistance;
struct path* from;
} path;
path* CreatePath(int yIndex, int xIndex, int distance) {
path *result = malloc(sizeof(path));
result->y = yIndex;
result->x = xIndex;
result->shortestDistance = distance;
result->from = NULL;
return result;
}
// Neighbor parsing
void addNeighbor(node **target, int neighborY, int neighborX, char pathType) {
int distance = 0;
int wasSet = 0;
if (pathType == '.') {
distance = 1;
wasSet = 1;
} else if (pathType == 'f') {
distance = 4;
wasSet = 1;
}
if (wasSet != 0) {
node *temp = *target;
(*target) = CreateNode(neighborY, neighborX, distance);
(*target)->next = temp;
}
}
int valueOf(queue *node, path ***targetPath) {
if (node) {
return targetPath[node->y][node->x]->shortestDistance;
} else {
return -1;
}
}
int main() {
// 0 - Only distance solution will be showing
// 1 - UI solution will be shown as well
int showSolution = 1;
char currentChar;
char **field;
int xIndex = 0, xLen = 1, yIndex = 0, yLen = 1;
// Allocates field with 1 row
field = malloc(yLen * sizeof(char **));
field[0] = malloc(xLen * sizeof(char *));
while (scanf("%c", ¤tChar) && currentChar != '-' && currentChar != '+') { // Reads input, until the first phase is complete
if (currentChar == '#' || currentChar == '.' || currentChar == 'f') {
field[yIndex][xIndex] = currentChar;
xIndex++;
if (yIndex == 0 && xIndex > xLen) { field[0] = realloc(field[0], (xLen *= 2) * sizeof(char *)); } // Doubles the number of columns - needed only on the first row
} else if (currentChar == '\n') {
if (yIndex == 0) { field[yIndex] = realloc(field[yIndex], (xLen = xIndex) * sizeof(char *)); } // Shirnks the first row - needed only on the first row
if (++yIndex >= yLen) { // If next index is out of allocated array
field = realloc(field, (yLen *= 2) * sizeof(char **));
}
field[yIndex] = malloc(xLen * sizeof(char *)); // Allocates next row
xIndex = 0;
}
}
free(field[yIndex]); // Frees last allocated row - is not needed
field = realloc(field, (yLen = yIndex) * sizeof(char **)); // Retrieves only needed rows
// Creates field with linked list of neighbors for each tile
node ***neighborsField = malloc(yLen * sizeof(node ***));
for (int y = 0; y < yLen; y++) { neighborsField[y] = malloc(xLen * sizeof(node **)); }
// Parsing 2D array
for (int yAxis = 0; yAxis < yLen; yAxis++) {
for (int xAxis = 0; xAxis < xLen; xAxis++) {
neighborsField[yAxis][xAxis] = NULL;
if (field[yAxis][xAxis] == '.' || field[yAxis][xAxis] == 'f') {
// Is path on the top
if (yAxis == 0) // If on the very top - pacman style
addNeighbor(&neighborsField[yAxis][xAxis], yLen - 1, xAxis, field[yLen - 1][xAxis]);
else
addNeighbor(&neighborsField[yAxis][xAxis], yAxis - 1, xAxis, field[yAxis - 1][xAxis]);
// Is path on the right
if (xAxis == xLen - 1) // If on the very right - pacman style
addNeighbor(&neighborsField[yAxis][xAxis], yAxis, 0, field[yAxis][0]);
else
addNeighbor(&neighborsField[yAxis][xAxis], yAxis, xAxis + 1, field[yAxis][xAxis + 1]);
// Is path on the bottom
if (yAxis == yLen - 1) // If on the very bottom - pacman style
addNeighbor(&neighborsField[yAxis][xAxis], 0, xAxis, field[0][xAxis]);
else
addNeighbor(&neighborsField[yAxis][xAxis], yAxis + 1, xAxis, field[yAxis + 1][xAxis]);
// Is path on the left
if (xAxis == 0) // If on the very left
addNeighbor(&neighborsField[yAxis][xAxis], yAxis, xLen - 1, field[yAxis][xLen - 1]);
else
addNeighbor(&neighborsField[yAxis][xAxis], yAxis, xAxis - 1, field[yAxis][xAxis - 1]);
}
}
}
// Second phase
int xCordStart, xCordEnd, yCordStart, yCordEnd;
while (scanf("%d %d %d %d", &yCordStart, &xCordStart, &yCordEnd, &xCordEnd) != EOF) {
char **solutionField;
if (showSolution) {
// Allocates solutionField
solutionField = malloc(yLen * sizeof(char **));
for (int i = 0; i < yLen; i++)
solutionField[i] = malloc(xLen * sizeof(char *));
// Copies data from field to solutionField
for (int y = 0; y < yLen; y++)
for (int x = 0; x < xLen; x++)
solutionField[y][x] = field[y][x];
}
if (field[yCordStart][xCordStart] != '.' || field[yCordEnd][xCordEnd] != '.') { // Coordinates do not exist - invalid path
printf("-1 - invalid path, check input file\n");
} else if (yCordStart == yCordEnd && xCordStart == xCordEnd && !showSolution) { // Coordinates are the same - no need to move
printf("0\n");
} else {
// Creates a queue
int queueLength = 1, queueAlloced = 100;
queue **heapQueue = malloc(queueAlloced * sizeof(queue **));
int **visited = malloc(yLen * sizeof(int **)); // Creates a 2D array to check whether the vertex has been already processed
path ***paths = malloc(yLen * sizeof(path ***)); // Creates a 2D array with paths
// Initializes default values to prevent access errors
for (int y = 0; y < yLen; y++) {
visited[y] = malloc(xLen * sizeof(int *));
paths[y] = malloc(xLen * sizeof(path **));
for (int x = 0; x < xLen; x++) {
visited[y][x] = 0;
paths[y][x] = NULL;
}
}
paths[yCordStart][xCordStart] = CreatePath(yCordStart, xCordStart, 0); // Creates starting tile path
visited[yCordStart][xCordStart] = 1; // Sets starting cord to visited, so it doesn't get staged by queue
heapQueue[0] = CreateQueue(yCordStart, xCordStart, 0);
while (heapQueue[0]) { // While there are items in queue
node *currentNeighbor = neighborsField[heapQueue[0]->y][heapQueue[0]->x];
while (currentNeighbor) { // While there are more neighbors
if (visited[currentNeighbor->y][currentNeighbor->x] != 1) { // If the tile hasn't been visited / enqueued yet
if (queueLength >= queueAlloced) { // Doubles the memory allocated to the binary heap, if needed
queueAlloced *= 2;
heapQueue = realloc(heapQueue, queueAlloced * sizeof(queue *));
}
// Enqueues and sets as visited
heapQueue[queueLength] = CreateQueue(currentNeighbor->y, currentNeighbor->x, currentNeighbor->distance);
visited[currentNeighbor->y][currentNeighbor->x] = 1;
// Creates path and sets its shortestDistance
paths[currentNeighbor->y][currentNeighbor->x] = CreatePath(currentNeighbor->y, currentNeighbor->x, paths[heapQueue[0]->y][heapQueue[0]->x]->shortestDistance + currentNeighbor->distance);
if (showSolution) {
paths[currentNeighbor->y][currentNeighbor->x]->from = paths[heapQueue[0]->y][heapQueue[0]->x];
}
// Bubbles up to prevent min heap value violations
int currentIndex = queueLength;
int currentParent = (currentIndex - 1) / 2;
while (currentParent >= 0 && paths[heapQueue[currentParent]->y][heapQueue[currentParent]->x]->shortestDistance > paths[heapQueue[currentIndex]->y][heapQueue[currentIndex]->x]->shortestDistance) {
queue *temp = heapQueue[currentParent];
heapQueue[currentParent] = heapQueue[currentIndex];
heapQueue[currentIndex] = temp;
currentIndex = currentParent;
currentParent = (currentIndex - 1) / 2;
}
queueLength++;
}
currentNeighbor = currentNeighbor->next; // Passes to the next neighbor
}
if (heapQueue[0]->y == yCordEnd && heapQueue[0]->x == xCordEnd) {
for (int index = 0; index < queueLength; index++) {
free(heapQueue[index]);
}
break;
}
// Swaps first node with last one
queue *temp = heapQueue[0];
heapQueue[0] = heapQueue[queueLength - 1];
heapQueue[queueLength - 1] = temp;
// Deletes last node
free(heapQueue[queueLength - 1]);
heapQueue[--queueLength] = NULL;
// Bubble down
int currentIndex = 0, leftChild = 1, rightChild = 2;
while (leftChild < queueLength && paths[heapQueue[currentIndex]->y][heapQueue[currentIndex]->x]->shortestDistance > paths[heapQueue[leftChild]->y][heapQueue[leftChild]->x]->shortestDistance || rightChild < queueLength && paths[heapQueue[currentIndex]->y][heapQueue[currentIndex]->x]->shortestDistance > paths[heapQueue[rightChild]->y][heapQueue[rightChild]->x]->shortestDistance) {
queue *temp = heapQueue[currentIndex];
if (heapQueue[rightChild] && valueOf(heapQueue[rightChild], paths) < valueOf(heapQueue[leftChild], paths)) { // If node has both children (there is right child)
heapQueue[currentIndex] = heapQueue[rightChild];
heapQueue[rightChild] = temp;
currentIndex = rightChild;
} else { // If node has only left child
heapQueue[currentIndex] = heapQueue[leftChild];
heapQueue[leftChild] = temp;
currentIndex = leftChild;
}
leftChild = currentIndex * 2 + 1;
rightChild = currentIndex * 2 + 2;
}
}
// Outputs the shortest distance
if (paths[yCordEnd][xCordEnd])
printf("%d\n", paths[yCordEnd][xCordEnd]->shortestDistance);
else
printf("-1\n");
if (showSolution) {
path* currentPath = paths[yCordEnd][xCordEnd];
while (currentPath && currentPath->from) {
solutionField[currentPath->y][currentPath->x] = 'x';
currentPath = currentPath->from;
}
solutionField[yCordStart][xCordStart] = 'S';
solutionField[yCordEnd][xCordEnd] = 'E';
// Prints out solutionField
for (int y = 0; y < yLen; y++) {
for (int x = 0; x < xLen; x++) {
if (solutionField[y][x] == 'x') {
printf("\033[1;3;40m%c\033[0m", field[y][x]);
} else if (solutionField[y][x] == 'S') {
printf("\033[1;3;40;33m%c\033[0m", solutionField[y][x]);
} else if (solutionField[y][x] == 'E') {
printf("\033[1;3;40;32m%c\033[0m", solutionField[y][x]);
} else {
printf("\033[2;40;37m%c\033[0m", solutionField[y][x]);
}
}
printf("\n");
}
printf("\n");
for (int i = 0; i < yLen; i++) { free(solutionField[i]); }
free(solutionField);
}
// Frees the path
for (int y = 0; y < yLen; y++) {
for (int x = 0; x < xLen; x++) {
if (paths[y][x]) {
free(paths[y][x]);
}
}
if (visited[y]) { free(visited[y]); }
if (paths[y]) { free(paths[y]); }
}
if (paths) { free(paths); }
if (visited) { free(visited); }
if (heapQueue) { free(heapQueue); }
}
}
// Default field free
for (int index = 0; index < yLen; index++) { free(field[index]); }
free(field);
// Neighbors field free
for (int yAxis = 0; yAxis < yLen; yAxis++) {
for (int xAxis = 0; xAxis < xLen; xAxis++) {
if (!neighborsField[yAxis][xAxis]) { continue; }
node *currentNode = neighborsField[yAxis][xAxis];
while (currentNode->next) {
node *temp = currentNode;
currentNode = currentNode->next;
free(temp);
}
free(currentNode);
}
free(neighborsField[yAxis]);
}
free(neighborsField);
return 0;
}