Treat an array of blocks as nodes in a graph, with the blocks above, below, to the right and left of each block as adjacent, and assign random edge weights. Then run Prim's minimum spanning tree algorithm. Voila! A block maze!

My youngest is... sort of impressed.

The spanning tree guarantees that there are no cycles, every square is accessible, and there is exactly one path from the start at the lower left to the end at the upper right.

Follow

The other way to do it would be to forget about the edge weights and just build the spanning tree by selecting a random edge from an already-connected block. But then I wouldn't have had to remember all that stuff about priority queues and heaps, and where's the fun in that?

· · Web · 0 · 0 · 0
Sign in to participate in the conversation
arktos.social

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!