MODERATOR NOTE: this work was submitted to Computational Art Contest 2022. Check out the contest and other submissions here: https://wolfr.am/CompArt-22
Fig. 1. Knight’s walks, colored by path count summed over all possible starting points along the four exterior edges.
We are happy to announce the release of RandomSierpinskiMaze via Wolfram Function Repository. The documentation already gives an example how to chart a knight’s walk through the maze, but for this post, let’s take our explanation and plotting to the next fractal level!
The states adjacency graph is produced quite readily as:
RandomSierpinskiMaze = ResourceFunction["RandomSierpinskiMaze"];
SeedRandom["Compare"]; maze0 = RandomSierpinskiMaze[3, AdjacencyGraph];
Graph[#, VertexCoordinates -> (# -> # & /@ VertexList[#])] &@maze0
This graph assumes that the maze explorer moves by \pm (0,1) or by \pm (1,0). With knight’s walks we allow and require that steps move by (\pm1,\pm2) or (\pm2,\pm1) for eight different choices of signs. (Knights are sometimes described as moving along a circle with radius \sqrt{5}.) The documentation also shows how to superimpose the states adjacency graph assuming the knight-walk transition function:
GraphUnion[maze0, kmaze0,
VertexCoordinates -> (# -> # & /@ VertexList[maze0]),
EdgeStyle -> Join[# -> Black & /@ EdgeList[maze0], # -> Orange & /@
EdgeList[kmaze0]]]
And if you look closely, you will see that knights can not achieve an ergodic limit when exploring possibilities of the maze. Their particular movement pattern locks them out of some corners, and makes it difficult to pass through narrow corridors.
Now we can find a difficult looking Knight’s walk (in coordinate-free form):
KnightWalkGraph[graph0_] := With[{knightsMoves =
Flatten[ Outer[#1@{#2 2, #3 1} &, {Reverse, Identity}, {1, -1}, {1, -1}], 2]},
Graph[Select[EdgeList[#], GraphDistance[graph0, #[[1]], #[[2]]] == 3 &]] &@
Graph[ Union[ Sort /@ Select[
Flatten[ Outer[UndirectedEdge[#1, #1 + #2] &, VertexList[maze0],
knightsMoves, 1]], MemberQ[VertexList[maze0], #[[2]]] &]]]]
kmaze0 = With[{graph0 = KnightWalkGraph[maze0]},
Graph[HighlightGraph[graph0,
PathGraph[FindShortestPath[graph0, {58, 13}, {83, 67}]]]]]
Plenty of opportunities in there to get lost and take a wrong turn, but if you do, who knows what you will find! Even shortest paths between point A and point B can involve a fair amount of exploration:
With[{knightPath = FindShortestPath[kmaze0, {71, 1}, {83, 67}],
arrayMaze = (SeedRandom["Compare"];
RandomSierpinskiMaze[3][[1, 1]]) },
animationData = ArrayPlot[ReplacePart[
Reverse[arrayMaze],
Join[Reverse[# {1, -1} + {1, -1}] -> 2 & /@ Flatten[
FindShortestPath[maze0, #1, #2][[2 ;; -2]] & @@@
Partition[#, 2, 1], 1],
Reverse[# {1, -1} + {1, -1}] -> 1 & /@ #] &@
knightPath[[1 ;; #]]],
PixelConstrained -> 8, ColorRules -> {
4 -> LightGray, 5 -> White, 0 -> Black, 1 -> Orange,
2 -> LightOrange
}] & /@ Range[Length[knightPath]];]
ListAnimate[animationData]

This is just one particularly nice path. There are still many more paths to explore through this maze, and through other mazes generated from different seeds. And yes, if you have a metaverse strategy, mazes can be depicted in three dimensions (also thank the referees asking for extra revisions):
CarpetFractal[n_] := Nest[ArrayFlatten[{{#, #, #}, {#, 1, #}, {#, #, #}}] &, {{0, 0,
0}, {0, 1, 0}, {0, 0, 0}}, n]
ArrayMesh[Join[With[
{mazebase = RandomSierpinskiMaze[3][[1, 1, 3 ;; -3, 3 ;; -3]
] /. {5 -> 0, 0 -> 1}}, Table[mazebase, {5}]], NestList[
RotateRight[ Map[First[Sort@Commonest[Flatten[#][[{2, 4, 6, 8}]]]] &,
Partition[#, {3, 3}, {1, 1}, {1, 1}], {2}], {1, 1}] &,
CarpetFractal[3], 3^3 - 1]][[1 ;; -1 ;; 2]], ImageSize -> 500,
ViewVertical -> {1, 0, 0}, ViewPoint -> 2 {1.5, 1.5, 1}]
Have fun & use more colors as desired! For example, here’s another interesting plot, where colors indicate that wall topology is different around top center:






– you have earned Featured Contributor Badge 

















