I am trying to understand recursive backtracking by creating an algorithm that finds a way out from a maze. So this is my "backtracking" logic:
1) Identify all open locations from current location that you haven't been to before ex: up, down. (left and right may be blocked by a wall or might have been visited before)
2) If amtOfOpenLocations >= 1 choose a random openLocation and move there.
3) (Recursive call) If nothing went wrong repeat #1 until a path out, or base case, is reached.
4) If amtOfMoveLocations < 1 step back through each move made until one of those moves has an available move location that is other than any of the moves already made. Repeat steps 1-3.
So my first question is: Is this a correct implementation of a backtracking algorithm?
If my logic is correct here is my next question:
So basically what I do is I keep checking locations other than the ones already made until I find a way out. When I find a way out I ignore all other possible moves and return the solution. If, for example, I am in location (4,4) and I have locations (4,3), (4,5), (5,4), (3,4) all available as openLocations do I make 4 recursive calls to each one of those locations or do I simply make one recursive call to anyone of them and test each location one by one?
My book says "If you are not at an exit point, make 4 recursive calls to check all 4 directions, feeding the new coordinates of the 4 neighboring cells."
An answer on stackoverflow about a similar problem says: "multiple moves per iteration...is wrong" So because of this I am confused. Is my book wrong or what?
Lastly what would be the optimal way to prevent locations that I already checked before? My idea is to keep a temporary array that has all visited locations marked by "!" and my getMoveLocations() method would avoid any cell with an "!". Is there a better way to do this or is my way acceptable?