Maze generation: Difference between revisions

m
Line 2,617:
Here I generate a maze as a graph. Vertices of the graph are cells and edges of the graph are removed walls. This version is mush faster and is convenient to solve.
<lang mathematica>MazeGraph[m_, n_] :=
Block[{$RecursionLimit = Infinity, grid = GridGraph[{m, n}],
unvisitedQ}, unvisitedQ[_] := True;
visited = {}},
Graph[Range[m n], Reap[{AppendTounvisitedQ[visited, #] = False;
Do[
If[FreeQunvisitedQ[visited, neighbor],
Sow[# <-> neighbor]; #0@neighbor], {neighbor,
RandomSample@AdjacencyList[grid, #]}]} &@
RandomChoice@VertexList@grid][[2, 1]],
GraphLayout -> {"GridEmbedding", "Dimension" -> {m, n}}]];
maze = MazeGraph[13, 21]</lang>
{{Out}}