O'Reilly logo

Artificial Intelligence with Python by Prateek Joshi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Building a maze solver

Let's use the A* algorithm to solve a maze. Consider the following figure:

Building a maze solver

The # symbols indicate obstacles. The symbol o represents the starting point and x represents the goal. Our goal is to find the shortest path from the start to the end point. Let's see how to do it in Python. The following solution is a variant of the solution provided in the simpleai library. Create a new Python file and import the following packages:

import math 
from simpleai.search import SearchProblem, astar 

Create a class that contains the methods needed to solve the problem:

# Class containing the methods to solve the maze class MazeSolver(SearchProblem): ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required