Reputation: 49
I am tasked with solving a maze using the method of backtracking and recursion. This is more of a conceptual question about this concept.
How is the backtracking call ever reached? From all the examples I have seen it looks as though recursion is always called immediately before the backtracking step, so that backtracking can not be reached. Can anyone explain to me how the backtracking step is reached?
Upvotes: 0
Views: 371
Reputation: 4955
Backtracking happens when the recursion returns. For example, if you are at location A, and there are two neighboring locations, B and C. You could make a recursive call on B. When it has finished exploring that part of the maze, it will return (ie, backtrack) to the call at location A, which will then make a new recursive call on location C.
Upvotes: 1