3

I have coded the A* Algorithm for a project. One of the requirements of this project is to randomly generate 50 mazes.

I am a bit stuck because this is not similar to normal maze generations. In maze generations you have blocked and unblocked walls, while in my case I need to have blocked and unblocked tiles. It also can't be perfect (there should be multiple paths). I haven't really been able to find an algorithm online or description that fits this case. What would be the best way to accomplish this? I also would like to specify a starting + ending point if possible, if not then just a starting point. Thank you!

This is an example maze I manually generated (on a smaller scale):

enter image description here

c0der
  • 15,550
  • 6
  • 26
  • 53
noobcoder24
  • 117
  • 8

2 Answers2

2

You can do this with a union-find data structure, similar to using Kruskal's algorithm to generate a maze:

  • Pick start and end points
  • Mark every cell except the start and end as blocked, and put each cell in its own set
  • Randomly unblock cells. When you unblock a cell, merge its set with the sets of any unblocked cells it's connected to.
  • Stop when the start cell's set is merged with the end cell's set.

There will now be a path from the start to the end. If you want to make sure that the maze is a little more open, you can keep randomly unblocking cells until, say, at least 70% are unblocked.

The result is not going to look a lot like a traditional maze, but it will probably be good for A* testing.

Matt Timmermans
  • 36,921
  • 2
  • 27
  • 59
1

You could randomly assign a value (that represents blocked or un-blocked) to each tile. After you do that assign a start and end point.
The result could look like this.
If you prefer to use maze-generation algorithms use one of the widely available resources like : 1 , 2

c0der
  • 15,550
  • 6
  • 26
  • 53