Maze generation: Difference between revisions
Content added Content deleted
(Removed the link to the second D entry) |
(Mathematica) |
||
Line 2,085: | Line 2,085: | ||
<a id='solve' style='display:none' href='javascript:solve(); void(0)'>Solve</a> |
<a id='solve' style='display:none' href='javascript:solve(); void(0)'>Solve</a> |
||
</fieldset></form><table id='maze'/></body></html></lang> |
</fieldset></form><table id='maze'/></body></html></lang> |
||
=={{header|Mathematica}}== |
|||
<lang mathematica>MazeGraphics[m_, n_] := |
|||
Block[{$RecursionLimit = Infinity, |
|||
unvisited = Tuples[Range /@ {m, n}], maze}, |
|||
maze = Graphics[{Line[{{#, # - {0, 1}}, {#, # - {1, 0}}}] & /@ |
|||
unvisited, |
|||
Line[{{0, n}, {0, 0}, {m, 0}}]}]; {unvisited = |
|||
DeleteCases[unvisited, #]; |
|||
Do[If[MemberQ[unvisited, neighbor], |
|||
maze = DeleteCases[ |
|||
maze, {#, |
|||
neighbor - {1, 1}} | {neighbor, # - {1, 1}}, {5}]; #0@ |
|||
neighbor], {neighbor, |
|||
RandomSample@{# + {0, 1}, # - {0, 1}, # + {1, 0}, # - {1, |
|||
0}}}]} &@RandomChoice@unvisited; maze]; |
|||
maze = MazeGraphics[13, 21]</lang> |
|||
{{Out}} |
|||
[[File:MathematicaMazeGraphics.png]] |
|||
===Graph=== |
|||
{{Works with|Mathematica|9.0}} |
|||
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}], |
|||
visited = {}, |
|||
edges = {}}, {AppendTo[visited, #]; |
|||
Do[If[FreeQ[visited, neighbor], |
|||
AppendTo[edges, # <-> neighbor]; #0@neighbor], {neighbor, |
|||
RandomSample@AdjacencyList[grid, #]}]} &@ |
|||
RandomChoice@VertexList@grid; |
|||
Graph[Range[m n], edges, |
|||
GraphLayout -> {"GridEmbedding", "Dimension" -> {m, n}}]]; |
|||
maze = MazeGraph[13, 21]</lang> |
|||
{{Out}} |
|||
[[File:MathematicaMazeGraph.png]] |
|||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |