Wolfram mathematica 如何从迷宫生成代码Mathematica中获取已删除墙的数组
我正试图从这个迷宫生成代码中找出所有被删除的墙的数组。似乎无法让它工作,当我要求它打印时,它只会给我整个迷宫网格,而不是我要求的特定墙Wolfram mathematica 如何从迷宫生成代码Mathematica中获取已删除墙的数组,wolfram-mathematica,maze,Wolfram Mathematica,Maze,我正试图从这个迷宫生成代码中找出所有被删除的墙的数组。似乎无法让它工作,当我要求它打印时,它只会给我整个迷宫网格,而不是我要求的特定墙 MazeGen2[m_, n_] := Block[{$RecursionLimit = Infinity, unvisited = Tuples[Range /@ {m, n}], maze, mazearray = {}, mazeA}, (*unvisited=Delete[unvisited,{{1},{2},{Lengt
MazeGen2[m_, n_] :=
Block[{$RecursionLimit = Infinity,
unvisited = Tuples[Range /@ {m, n}], maze, mazearray = {},
mazeA},
(*unvisited=Delete[unvisited,{{1},{2},{Length[
unvisited]-1},{Length[unvisited]}}];*)
(*Print[unvisited];*)
maze = {{{{#, # - {0, 1}}, {#, # - {1, 0}}}} & /@
unvisited, {{{0, n - 1}, {0, 0}, {m - 1,
0}}}};(*This generates the grid*)
Print[maze];
{unvisited = DeleteCases[unvisited, #];
(*Print[unvisited];*)
Do[
If[MemberQ[unvisited, neighbor],
maze = DeleteCases[
maze, {#, neighbor - {1, 1}} | {neighbor, # - {1, 1}}, {5}]
(*mazeA=Flatten[AppendTo[mazearray,
maze]];*)
; #0@neighbor],
{neighbor,
RandomSample@{# + {0, 1}, # - {0, 1}, # + {1, 0}, # - {1,
0}}}
]
} &@RandomChoice@unvisited;
Flatten[maze]
];
我在网站上找到了你的代码,谢谢你下面是如何使用基于图形的替代方法生成迷宫。这是用户AlephAlpha提供的:
MazeGraph[m_, n_] :=
Block[{$RecursionLimit = Infinity, grid = GridGraph[{m, n}],
visited = {}},
Graph[Range[m n],
Reap[{AppendTo[visited, #];
Do[
If[FreeQ[visited, neighbor],
Sow[# <-> neighbor]; #0@neighbor],
{neighbor, RandomSample@AdjacencyList[grid, #]}]} & @
RandomChoice@VertexList@grid][[2, 1]],
GraphLayout -> {"GridEmbedding", "Dimension" -> {m, n}},
EdgeStyle -> Directive[Opacity[1], AbsoluteThickness[12], Purple],
VertexShapeFunction -> None,
VertexLabels -> "Name",
VertexLabelStyle -> White,
Background -> LightGray,
ImageSize -> 300]];
width = height = 8;
maze = MazeGraph[width, height]
同样很容易找到被删除的墙-这里是原始GridGraph
和迷宫之间的graph差异:
hg = HighlightGraph[
GridGraph[{width, height},
EdgeStyle ->
Directive[Opacity[0.2], Blue, AbsoluteThickness[1]]],
EdgeList[GraphDifference[GridGraph[{width, height}], maze]],
Background -> LightGray,
ImageSize -> 300,
GraphHighlightStyle -> {"Thick"}];
显示所有三个:
Row[{Labeled[maze, "maze"], Spacer[12], Labeled[hg, "deleted walls"],
Labeled[solution, "solution"]}]
为样式问题道歉-这是使用图形的困难部分…:) 我懒得剪切和粘贴包含注释掉的代码的代码片段,这些代码的存在只会让您的函数更难理解。我懒得阅读@HighPerformanceMark的注释。我建议您(尝试)编写代码来绘制迷宫。你会发现你的初始网格被弄乱了。注意这段代码是从这里借来的:并且严重损坏了。希望没有其他人会浪费时间来修复它。(我不敢相信这个问题没有被堵住)这条路不应该在墙之间吗?(与罗塞塔代码版本相比)…@george正如我所见,墙壁是灰色的,走廊是紫色的,解决方案是白色的。因此,删除的墙实际上是删除的走廊:)
Row[{Labeled[maze, "maze"], Spacer[12], Labeled[hg, "deleted walls"],
Labeled[solution, "solution"]}]