Without commenting on your specific solution, you can think of reusing the first depth search as a standard algorithm, which, I think, you will agree, is somewhat more clear:
boolean dfs (State s) { if (end_state(s)) { return true; } vector<option_t> options = generate_moves(s); for (vector<option_t>::iterator it = options.begin(); it != options.end(); it++) { make_move(s, it); boolean found = dfs(s); if (found) { cout << s << endl; } undo_move(s, it); if (found) return true; } return false; }
As you can see, this will work as long as you can create:
- some kind of class state that contains the current state of the maze
- End_state function that knows when State is in solution
- Function generate_moves, which can find all parameters of the current state
- make_move, which can apply the transition to your state.
- undo_move, which can cancel the motion against the state
- you define option_t so that it represents a move option
- define the operator <<for the state
I can tell you that, of course, the solution that I give you has no problems with printing backspaces (it should be clear from the code too).
Kevin source share