Random Acyclic Maze Generator with given Entry and Exit point using Python

Introduction:

In this tutorial, we learn about random Acyclic Maze Generator with given Entry and Exit points using Python. Here, we need to take two integer values, A and B. The main task is creating a maze containing size A * B with only 0 and 1 with entry point P0 and exit point P1. 0 represents a wall, and 1 represents the free space that can be moved. Between any two movable positions must contain a path. P0 is marked as 2, and P1 is marked as 3. It can move in 4 directions (up, down, right, left) moving position.

Example:

Now, we give an example of a random Acyclic Maze Generator with a given Entry and Exit point by taking an input and sharing its output. So, the example is now given in below -

Algorithm:

Now we learn about the algorithm of random Acyclic Maze Generator with given Entry and Exit point using Python. The steps are given in below -

  1. For the iterative DFS, stack (S) is initialized. The matrix returns the random maze.
  2. P0 is the entry point. It is inserted into the stack.
  3. Repeat the following steps until the stack is not empty:
    1. Remove the position from stack (S). Then mark it as seen.
    2. If the position marker is walkable, create a loop and exclude it as a moveable position. Otherwise, it sets the position as walkable.
    3. Put the unvisited neighbours in P into the stack in random order.
    4. Random insertion into the stack ensures that the generated maze is random.
    5. If a neighbour is similar to P1, then insert this neighbour at the top of the stack. For that, we do not skip this position because of the loop creation.
  4. P0 is marked by 2, which is the initial position of the stack. P1 is marked by 3, which is the final position.
  5. Lastly, the maze is returned.

Program code:

Here, we use Python to give the program code of a random Acyclic Maze Generator with given Entry and Exit points. The code is now given below -

Output:

Now, we compile the above code in Python, and after successful compilation, we run it. The output is given below -

[1, 3, 1, 0, 1, 1]
[1, 0, 1, 1, 1, 0]
[0, 1, 0, 0, 1, 1]
[1, 1, 1, 0, 1, 0]
[1, 0, 0, 1, 1, 1]
[1, 1, 1, 1, 0, 4]