-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaze.cpp
More file actions
104 lines (93 loc) · 2.78 KB
/
Copy pathMaze.cpp
File metadata and controls
104 lines (93 loc) · 2.78 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
#include "Maze.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <bits/stdc++.h>
Maze::Maze(int **matrix, int height, int width)
{
this->matrix = matrix;
this->height = height;
this->width = width;
}
std::vector<int *> *Maze::solve(int start[], int end[])
{
// vector to store the coordinates and to return to the funciton caller
std::vector<int *> *coordinates = new std::vector<int *>();
// my matrix of coordinates for the solution
int ***solution = (int ***)malloc(height * sizeof(int **));
for (int i = 0; i < height; i++)
{
// Assign to array[i], not *array[i] (that would dereference an uninitialized pointer)
solution[i] = (int **)malloc(width * sizeof(int *));
for (int j = 0; j < width; j++)
{
solution[i][j] = (int *)malloc(3 * sizeof(int));
solution[i][j][0] = -1;
}
}
int before[2] = {0, 0};
recursiveSolve(solution, before, start, end, 0);
if (solution[end[0]][end[1]][0] == -1)
{
return coordinates;
}
int size = solution[end[0]][end[1]][0];
int x = end[0];
int y = end[1];
for (int i = size; i >= 0; i--)
{
int *curr = new int[2]{x, y};
coordinates->push_back(curr);
int nX = solution[x][y][1];
int nY = solution[x][y][2];
x = nX;
y = nY;
}
std::reverse(coordinates->begin(), coordinates->end());
return coordinates;
}
void Maze::recursiveSolve(int ***solution, int before[], int start[], int finish[], int length)
{
if (start[0] < 0 || start[0] >= width || start[1] < 0 || start[1] >= height)
{ // out of bounds
return;
}
if (matrix[start[0]][start[1]] == 1)
{ // hits a wall
return;
}
if (solution[start[0]][start[1]][0] == -1)
{ // new node hit
solution[start[0]][start[1]][0] = length;
solution[start[0]][start[1]][1] = before[0];
solution[start[0]][start[1]][2] = before[1];
}
else
{
if (solution[start[0]][start[1]][0] < length)
{ // loops back with longer path
return;
}
else
{ // loops back shorter
solution[start[0]][start[1]][0] = length;
solution[start[0]][start[1]][1] = before[0];
solution[start[0]][start[1]][2] = before[1];
}
}
if (start[0] == finish[0] && start[1] == finish[1])
{ // finds the end
return;
}
int k[] = {start[0], start[1]};
k[1]++;
recursiveSolve(solution, start, k, finish, length + 1);
k[1] -= 2;
recursiveSolve(solution, start, k, finish, length + 1);
k[1]++;
k[0]++;
recursiveSolve(solution, start, k, finish, length + 1);
k[0] -= 2;
recursiveSolve(solution, start, k, finish, length + 1);
return;
}