我想從這個迷宮代生成所有刪除牆的數組。似乎無法讓它工作,當我問它打印它只會給我整個迷宮網格,而不是我要求的具體牆壁。如何從迷宮生成代碼中獲取已刪除牆的數組Mathematica
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]];*)
; #[email protected]],
{neighbor,
[email protected]{# + {0, 1}, # - {0, 1}, # + {1, 0}, # - {1,
0}}}
]
} &@[email protected];
Flatten[maze]
];
我太懶惰剪切和粘貼一個包含註釋掉代碼的代碼片段,它們的存在會讓您的函數更難理解。 –
我懶得讀@HighPerformanceMark的評論 –
我建議你(嘗試)編寫代碼來繪製你的迷宮。你會發現你的初始網格被糾纏起來。 – agentp