Solving mazes using Python: Simple recursivity and A* search

problem solving maze game

This post describes how to solve mazes using 2 algorithms implemented in Python: a simple recursive algorithm and the A* search algorithm.

The maze we are going to use in this article is 6 cells by 6 cells. The walls are colored in blue. The starting cell is at the bottom left (x=0 and y=0) colored in green. The ending cell is at the top right (x=5 and y=5) colored in green. We can only move horizontally or vertically 1 cell at a time.

Python algorithms for mazes

Recursive walk

We use a nested list of integers to represent the maze. The values are the following:

  • 0: empty cell
  • 1: unreachable cell: e.g. wall
  • 2: ending cell
  • 3: visited cell

This is a very simple algorithm which does the job even if it is not an efficient algorithm. It walks the maze recursively by visiting each cell and avoiding walls and already visited cells.

The search function accepts the coordinates of a cell to explore. If it is the ending cell, it returns True. If it is a wall or an already visited cell, it returns False. The neighboring cells are explored recursively and if nothing is found at the end, it returns False so it backtracks to explore new paths. We start at cell x=0 and y=0.

Let’s see what happens when we run the script.

First cell visited is (0,0). Its neighbors are explored starting by the one on the right (1,0). search(1,0) returns False because it is a wall. There is no cell below and on the left so the one at the top (0,1) is explored. Right of that is a wall and below is already visited so the one at the top (0,2) is explored. This is what we have so far:

Python algorithms for mazes

Because the neighbor on the right is explored first, this algorithm is going to explore the dead-end at the bottom-right first.

Python algorithms for mazes

The algorithm is going to backtrack because there is nothing else to explore as we are in a dead-end and we are going to end up at cell (1, 2) again where there is more to explore.

Python algorithms for mazes

Let’s continue, we end up in a second dead-end at cell (4, 2).

Python algorithms for mazes

Backtracking happens one more time to go back to cell (5, 3) and we are now on our way to the exit.

Python algorithms for mazes

The full walk looks like this:

Python algorithms for mazes

We are going to look at a more sophisticated algorithm called A* search. This is based on costs to move around the grid. Let’s assume the cost to move horizontally or vertically 1 cell is equal to 10. Again, we cannot move diagonally here.

Before we start describing the algorithm, let’s define 2 variables: G and H. G is the cost to move from the starting cell to a given cell.

Python algorithms for mazes

H is an estimation of the cost to move from a given cell to the ending cell. How do we calculate that if we don’t know the path to the ending cell? To simplify, we just calculate the distance if no walls were present. There are other ways to do the estimation but this one is good enough for this example.

Python algorithms for mazes

We use 2 lists: an open list containing the cells to explore and a closed list containing the processed cells. We start with the starting cell in the open list and nothing in the closed list.

Let’s follow 1 round of this algorithm by processing our first cell from the open list. It is the starting cell. We remove it from the list and append it to the closed list. We retrieve the list of adjacent cells and we start processing them. The starting cell has 2 adjacent cells: (1, 0) and (0, 1). (1, 0) is a wall so we drop that one. (0, 1) is reachable and not in the closed list so we process it. We calculate G and H for it. G = 10 as we just need to move 1 cell up from the starting cell. H = 90: 5 cells right and 4 cells up to reach the ending cell. We call the sum F = G + H = 10 + 90 = 100. We set the parent of this adjacent cell to be the cell we just removed from the open list: e.g. (0, 0). Finally, we add this adjacent cell to the open list. This is what we have so far. The arrow represents the pointer to the parent cell.

Python algorithms for mazes

We continue with the cell in the open list having the lowest F = G + H. Only one cell is in the open list so it makes it easy. We remove it from the open list and we get its adjacent cells. Again, only one adjacent cell is reachable: (0, 2). We end up with the following after this 2nd round.

Python algorithms for mazes

3nd round result looks like this. Cells in green are in the open list. Cells in red are in the closed list.

Python algorithms for mazes

Let’s detail the next round. We have 2 cells in the open list: (1, 2) and (0, 3). Both have the same F value so we pick the last one added which is (0, 3). This cell has 3 reachable adjacent cells: (1, 3), (0, 2) and (0, 4). We process (1, 3) and (0, 4). (0, 2) is in the closed list so we don’t process that one again. We end up with:

Python algorithms for mazes

Let’s fast forward to:

Python algorithms for mazes

We have (1, 2), (1, 3) and (3, 3) in the open list. (1, 3) is processed next because it is the last one added with the lowest F value = 100. (1, 3) has 1 adjacent cell which is not in the closed list. It is (1, 2) which is in the open list. When an adjacent cell is in the open list, we check if its F value would be less if the path taken was going through the cell currently processed e.g. through (1, 3). Here it is not the case so we don’t update G and H of (1, 2) and its parent. This trick makes the algorithm more efficient when this condition exists.

Let’s take a break and look at a diagram representing the algorithm steps and conditions:

Python algorithms for mazes

We continue processing the cells remaining in the open list. Fast forward to:

Python algorithms for mazes

We have 2 cells in the open list: (3, 3) and (2, 0). The next cell removed from the open list is (3, 3) because its F is equal to 120. This proves that this algorithm is better than the first one we saw. We don’t end up exploring the dead end at (5, 0) and we continue walking from (3, 3) instead which is better.

Fast forward again to:

Python algorithms for mazes

The next cell processed from the open list is (5, 5) and it is the ending cell so we have found our path. It is easy to display the path. We just have to follow the parent pointers up to the starting cell. Our path is highlighted in green on the following diagram:

Python algorithms for mazes

You can read more about this algorithm here .

A* implementation

The basic object here is a cell so we write a class for it. We store the coordinates x and y, the values of G and H plus the sum F.

Next is our main class named AStar. Attributes are the open list heapified (keep cell with lowest F at the top), the closed list which is a set for fast lookup, the cells list (grid definition) and the size of the grid.

We create a simple method initializing the list of cells to match our example with the walls at the same locations.

Our heuristic compute method:

We need a method to return a particular cell based on x and y coordinates.

Next is a method to retrieve the list of adjacent cells to a specific cell.

Simple method to print the path found. It follows the parent pointers to go from the ending cell to the starting cell.

We need a method to calculate G and H and set the parent cell.

The main method implements the algorithm itself.

You can checkout the code on GitHub: git clone https://[email protected]/laurentluce/python-algorithms.git.

That’s it for now. I hope you enjoyed the article. Please write a comment if you have any feedback.

' src=

Excellent article. I am thinking of making it required reading for my AI class. Many thanks.

' src=

A big thanks to you ! I’m studying in France and i need to study passfinding with a A star algorithm. And i understand what you do with your code ! Cordialy, a new fan !

' src=

Thank you for this very_nice explanation, and for your code :). A lil bit more to work on the pep8, but it’s fine by me :D. Anyhow, many thanks again.

' src=

Best Python A* I have found. I would recommend that you publish source in one file. I was able to adopt this into code very quickly. I think it is an example of sane/good OO code oriented toward a Cartesian grid system. Which I imagine would be the starting point for most people exploring A*. Would be better if Cell.g were less hardwired (to implement diagonal cost.)

' src=

I would like to read a follow up of this same article with the Jump Point Search.

In update_cell, you assign adj.g = cell.g + 10. But, this assumes only 4 adjacent cells. If you change get_adjacent_cells to allow diagonal steps, the diagonal increment would be 14. The problem is, update_cell doesn’t know which step is taken. I suggest adding the step to g in get_adjacent_cells. Also, why do you add 10 to the test on 21? if c.g > cell.g + 10:

' src=

@bruce: I indicate at the beginning of the post that only horizontal and vertical moves are allowed. Regarding “if c.g > cell.g + 10”, we are checking if the path going through the current cell is better than what was previously calculated for the adjacent cell. +10 means current path beats adjacent cell path + one move.

' src=

Hello, Thanks for this great tutorial. It really helped me understand the A* method. I know this post is now more than a year old, but I followed it now and noticed something that maybe I do not understand right or might be interesting for others to know: In the process function on line 17, I think that ‘if c in self.op:’ can never be true since self.op is actually a list of tuples, not a list of cell objects. This leads to the same cell being added multiple times to the open list and eventually slows the algorithm down or even creates an infinity loop in some situations. Am I missing something here or is this really a bug?

@Markus: Thanks for noticing this bug in the code. I updated the post.

' src=

Wonderfully explained.

' src=

Thanks for the detailed tutorial!

' src=

I wonder how the algorithm would be affected by weave mazes? http://weblog.jamisbuck.org/2011/3/4/maze-generation-weave-mazes

' src=

Excellent article and great explanation. Maybe grid numbers next to the images would help when looking up coordinates repeatedly? Otherwise keep up the python articles!

' src=

Excellent description and implemention of A*, well explained. Takes me back to my second year of university.

' src=

This is very nice. Good read! cheers James

@James: Thanks!

Comments are closed.

IMAGES

  1. HOW MAZE GAME BENEFIT YOUR KID PROBLEM SOLVING SKILLS

    problem solving maze game

  2. Teach Problem-Solving with Our Fun Maze Game for Children!

    problem solving maze game

  3. Teach Problem-Solving with Our Fun Maze Game for Children!

    problem solving maze game

  4. 24 DIY Problem Solving Mazes For Kids (Craft Gossip)

    problem solving maze game

  5. HOW MAZE GAME BENEFIT YOUR KID PROBLEM SOLVING SKILLS

    problem solving maze game

  6. Mazes For Kids Ages 8-12: Maze Activity Book 8-10, 9-12, 10-12 year

    problem solving maze game

VIDEO

  1. Maze Solver

  2. Fill maze with liquid

  3. Solve Mysterious Maze 1

  4. 🔍Find The Answer (Maze Challenge) -🕹Marble Maze Puzzle Game

  5. Maze Game Part 1

  6. A Maze a Day: brain booster exercises #easysolving #speedsolving #brainexercise

COMMENTS

  1. Build a Maze Solver in Python Using Graphs

    In this step-by-step project, you'll build a maze solver in Python using graph algorithms from the NetworkX library. Along the way, you'll design a binary file format for the maze, represent it in an object-oriented way, and visualize the solution using scalable vector graphics (SVG).

  2. Mazes and Keys | Math Playground

    Enter each maze and find your way to the star. Collect the keys to open locked color-coded doors. Move through the maze with arrow keys, WASD keys or touch. There are 10 mazes to complete. MP1 - Make sense of problems and persevere in solving them. Play Mazes and Keys at Math Playground! Can you unlock the doors and escape?

  3. Solving Mazes with Artificial Intelligence: A Python Tutorial

    Navigating through mazes might seem like a simple task at first glance, but it introduces fundamental concepts in artificial intelligence and algorithms. This tutorial explores how we can use...

  4. Solving mazes using Python: Simple recursivity and A* search

    Solving mazes using Python: Simple recursivity and A* search Laurent Luce written 14 years ago This post describes how to solve mazes using 2 algorithms implemented in Python: a simple recursive algorithm and the A* search algorithm.

  5. Python’s Path Through Mazes: A Journey of Creation and Solution

    But did you know that you can create and solve mazes programmatically using Python? In this blog post, we’ll walk you through the process, step by step. The basis of our maze generation is...

  6. Python maze solver - Maze-solving algorithms! How these int…

    In this section, we'll explore the basics of maze solving, understanding the intricacies of mazes and labyrinths, and how algorithms can help us navigate these complex networks. Imagine standing at the entrance of a maze, surrounded by towering walls. Your goal is to reach the end, but the path is not clear.