问题
In an interview, my interviewer asked me this question:
Develop a function to generate a random maze
This is a quite difficult question to solve in 30min if you don't have solved this question before. On the internet there are many solution but none seems easy. The method should respect this constraint:
- it should accept the size of the maze (a square maze NxN)
- it should consist of only Walls and Path
- to make it simple, the algorithm doens't need to set the entry and the exit
I will post the answer with the solution so other people will be able to find this question.
Example of a generated maze:
. # # . # . . . . .
. . . . . . # # # .
# . # # # # . . . .
. # . . . . # . # .
. # . # # . # . # .
. # . # . . # . . #
. . . # . # . # . .
. # . # . . . # # .
# . . . # . # . . .
. . # . # . . . # .
回答1:
A simple randomized DFS can be used to generate the maze. To start a full walled maze is initialized to the size of NxN, then this function traverse the maze and adds a path whenever possibile:
function generateMaze(&$maze, $point) {
$dirs = [
[0,-1],
[1,0],
[0,1],
[-1,0],
];
shuffle($dirs);
foreach($dirs as $dir) {
$newPoint = [$point[0] + $dir[0], $point[1] + $dir[1]];
if (isGoodPath($maze, $newPoint)) {
$maze[$newPoint[0]][$newPoint[1]] = '.';
generateMaze($maze, $newPoint);
}
}
return $maze;
}
The key to solve this is a good implementation of the function isGoodPath()
this function just checks if the new path is inside the maze and if we can remove the walls (that is we cannot have two parallel adjacent "free" path)
You can run the full implementation here: https://ideone.com/oufifB
A 25x25 maze:
# . . # . . . # . . . . . # . . . . . . . # . # .
. # . # # # . . . # # # . . # # . # # # . . . . .
. # . . . . # . # . . . # . . . # . . . . # . # .
. . # # # . # . . # . # # # # . . . # # . # . . #
. # . . # . . # . # . . # . # # # # . . # . # . .
. . # . # # . # . . # . . . . . . # . # . . . # .
# . . . . # . . # . . # . # . # # . . . . # # . .
. . # # . . # . . # . # . # . . . . # # . . # . #
. # . . # . # . # . . # . . # # . # . . # . # . .
. . . # . . # . . . # . . # . # . # . # . . . # .
# # . # . # . # # # . # # . . . . # . # . # # . .
. . . # . . . . . . . . # . # # # # . . . # # . #
# # # . . # # # # . # . . . # . . . . # # . . . #
. . . # # . . . . # # . # . # . # # # # # . # # .
. # . . . . # # . # . . # . # . . . . # . . # . .
. . # . # # . . . . # # # . . # # # # . . # # # .
# . . # . . . # # . . . . # . # . . . . # . # # .
. # . . # . # . # # . # . . . # . # # # . . . . .
. . # . . # . . . . # . # # . # . # . . # # . # .
# . . # . . . # # . # . . . . # . . # . . . . # .
. . # . . # # . # . . # # . # . # . . # . # # . .
# . . . # . . . . # . . . # . . # # . # . # . # #
# # . # . . # . # . # # . # # . . . . # . . . . .
# . . . # # # . . . # # . . # # . # # . # # # # .
. . # . . . . . # . . . # . . . . . . . . . . . .
If you want a "prettier" maze you can simply add full walls to the border of the maze
来源:https://stackoverflow.com/questions/36592057/simple-implementation-of-a-maze-generation-method-random-dfs