1

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;
  }
}
5
  • The first thing I notice is that you are making recursive calls without using the true / false return value. If the maze is solved, the calling function does not know about it. Oct 30, 2020 at 16:53
  • Is it possible for you to elaborate on that? I do not quite understand what you mean by using the true/false return. If the maze is solved, which means my is_goal boolean function returns true, then the original search_maze function returns true as well.
    – Ken
    Oct 30, 2020 at 16:55
  • I mean the incarnation that recursed, does not know that. You have search_maze(maze, track, rows, cols, x, y - 1, steps); I suggest bool result = search_maze(maze, track, rows, cols, x, y - 1, steps); and then take appropriate action. You can't end a recursion without returning through the call stack, cell by cell. The intermediate call layers are not skipped just by returning true, that is not how recursion works. Oct 30, 2020 at 16:56
  • capture your return and check it
    – RisingSun
    Oct 30, 2020 at 17:03
  • Thank you for the advice!! I've tried it out again, but for some reason it's not working for me, maybe I did not use what was mentioned properly. I've added the said changes in my code.
    – Ken
    Oct 31, 2020 at 2:07

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.