Maze generation: Difference between revisions
Content added Content deleted
(Mathematica) |
|||
Line 2,101: | Line 2,101: | ||
RandomSample@{# + {0, 1}, # - {0, 1}, # + {1, 0}, # - {1, |
RandomSample@{# + {0, 1}, # - {0, 1}, # + {1, 0}, # - {1, |
||
0}}}]} &@RandomChoice@unvisited; maze]; |
0}}}]} &@RandomChoice@unvisited; maze]; |
||
maze = MazeGraphics[ |
maze = MazeGraphics[21, 13]</lang> |
||
{{Out}} |
{{Out}} |
||
[[File:MathematicaMazeGraphics.png]] |
[[File:MathematicaMazeGraphics.png]] |
||
Line 2,109: | Line 2,109: | ||
<lang mathematica>MazeGraph[m_, n_] := |
<lang mathematica>MazeGraph[m_, n_] := |
||
Block[{$RecursionLimit = Infinity, grid = GridGraph[{m, n}], |
Block[{$RecursionLimit = Infinity, grid = GridGraph[{m, n}], |
||
visited = {}, |
visited = {}}, |
||
Graph[Range[m n], Reap[{AppendTo[visited, #]; |
|||
Do[ |
Do[ |
||
If[FreeQ[visited, neighbor], |
|||
Sow[# <-> neighbor]; #0@neighbor], {neighbor, |
|||
RandomSample@AdjacencyList[grid, #]}]} &@ |
RandomSample@AdjacencyList[grid, #]}]} &@ |
||
RandomChoice@VertexList@grid |
RandomChoice@VertexList@grid][[2, 1]], |
||
Graph[Range[m n], edges, |
|||
GraphLayout -> {"GridEmbedding", "Dimension" -> {m, n}}]]; |
GraphLayout -> {"GridEmbedding", "Dimension" -> {m, n}}]]; |
||
maze = MazeGraph[13, 21]</lang> |
maze = MazeGraph[13, 21]</lang> |