i want to implement the Recursive backtracker algorithm to solve maze problem, but i cant understand 2.3 Command ("remove the wall between the current cell and the chosen cell") would any help me ?
- Mark the current cell as 'Visited'
- If the current cell has any neighbours which have not been visited
- Choose randomly one of the unvisited neighbours
- add the current cell to the stack
- r开发者_运维百科emove the wall between the current cell and the chosen cell
- Make the chosen cell the current cell
- Recursively call this function
- else
- remove the last current cell from the stack
- Backtrack to the previous execution of this function
Edit In fact I want an algorithm to solve maze problem by using stack.
That algorithm is a maze generator not a maze solver. The idea is that you want to create a random maze. You also want all points in the maze to be reachable from all other points.
If you just randomly remove walls it is likely that your maze will not be connected. The recursive backtracking algorithm takes care of this by creating a random walk and removing the walls along that random walk. The recursive backtracking part allows you to walk to every cell in the maze, even when you reach a dead end.
Your algorithm is for god
mode. Normally you should do
- If the current cell is the exit, then finished
- If the current cell has any neighbours which have not been visited that are not walls
- Choose randomly one of the unvisited non-wall neighbours
- add the current cell to the stack
- nothing
- Make the chosen cell the current cell
- Recursively call this function
- else
- remove the last current cell from the stack
- Backtrack to the previous execution of this function
Removing the wall simply means removing the wall! You start with a grid of cells, each of which is totally surrounded by 4 walls. As you move randomly around (2.1) you remove the wall joining the cells.
精彩评论