Edit: The exit of the maze is only found when there is an empty spot without any walls along the borders of the maze.
Edit2: I've changed the code a bit after receiving the help, but still encountered the same issue. Could be that I did not use what was mentioned properly. I've changed the snippet of my code to just one recursion (the moving up portion) to minimize unnecessary repetition when viewing the snippet.
(Still kind of new to coding, so hope you don't mind) I am attempting to code for a program that prints the movement of a person finding a path through a given maze.
However, I've encountered a problem where after reaching the exit of the maze, the person walks back to the original position where he is at.
The original position is given by an (x, y) coordinate.
Here's a snippet of my function which finds the path through the maze:
bool find_path(char **maze, long **track, long rows, long cols, long x, long y, long steps)
{
bool found;
if (goal_found(maze, rows, cols, x, y)) {
return true;
}
track[y][x] = 2;
if (maze[y - 1][x] == EMPTY && track[y - 1][x] == 1) { // Move Up
steps++;
found = find_path(maze, track, rows, cols, x, y - 1, steps);
if (found) {
return true;
}
++steps;
}
}
Your program finds all paths through the maze; if you want to stop it, you need to change your invocations to stop traversing the maze if you hit success:
found = search_maze(maze, track, rows, cols, x, y + 1, steps);
swap(maze, y, x, y + 1, x);
if (found) {
return found;
}
At all of the recursive invocations.