Click here to Skip to main content
12,948,130 members (84,387 online)
Click here to Skip to main content
Add your own
alternative version


90 bookmarked
Posted 12 Dec 2004

Maze Solver (shortest path finder)

, 1 Mar 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
An article on finding shortest path in a 2D maze.

Maze Solver


The article presents a simple technique to find the shortest path between two points in a 2D Maze. Similar applications use graphs in such situations but this article shows how this can be done without the headache of graphs. It uses a technique similar to breadth-first search. The MazeSolver class stores the Maze as a 2D integer array with value '0' for open (available) nodes and non-zero for closed nodes (walls). If a path is to be found, a new 2D integer array is created with the path traced by PathCharacter whose default value is '100'. The class can also trace diagonal paths if it is allowed to do so.

Throughout this article, we will use "node" to refer elements of the matrix (2D integer array representing a maze). I assume that reader is familiar with graphs and its terminologies (edges, nodes, etc).

Breadth-First Search Algorithm

The general idea behind a breadth-first search algorithm is that we examine a starting node, say A. Then we examine all the neighbors of A, then we examine all the neighbors of all the neighbors of A, and so on.., until we reach the desired ending node or until we have no nodes to examine (in this case no path will exist). Naturally, we need to keep a track of all the nodes to assure that no node is processed more than once. This is accomplished by linking a field "status" with all the nodes. The outline of the algorithm is as follows:

  1. Initialize all nodes to the ready state (Status=Ready)
  2. Put the starting node, say A, in Queue and change its status to the waiting state (Status=Waiting)
  3. Repeat steps a and b until Queue is empty:
    1. Remove the front node, say N, of Queue. Process N and change the status of N to the processed state (Status=Processed)
    2. Add to the rear of Queue all the neighbors of N that are in the ready state (Status=Ready), and change their status to the waiting state (Status=Waiting)
  4. Exit

Shortest Path using the above algorithm

A minimum path between two nodes can be found using breadth-first search if we keep track of the origin of each edge (i.e. how we reach a particular element in the maze) by using an array Origin together with the array Queue. This method is used in the class.

Without Graphs!

Yes, the class uses breadth-first search technique without actual implementation of graphs. i.e. there is no class/struct used for graphs, no adjacency lists, no weights assigned to edges, etc. The only thing is mathematics; the class uses certain mathematical formulae to access the adjacent nodes of an element (node) in the matrix (maze). Lets see how it is done.

Node numbers

First, we assign node numbers to every element of the array starting from 0 to 'RowsXCols-1' in the manner below.

Maze Solver

Methods below can be used to transform between node number and indexes of a matrix

int GetNodeNo(int matrix, int ithIndex, int jthIndex)
    return ( ithIndex*matrix.GetLength(1)+ jthIndex );

void GetMatrixIndexes(int matrix, int iNodeNo, out ithIndex, out jthIndex)
    int iCols=matrix.GetLength(1);

The class, however, doesn't use separate methods for these transformations, they are done directly.

To apply the above algorithm, the class uses two integer array; Queue and Origin. A dummy array (equivalent to that of maze) is used to hold the current status of the respective nodes in the maze. To examine a node's adjacent nodes, we have to examine its left, right, top, bottom and 4 diagonal nodes (if diagonals are also to be searched).

Accessing Adjacent Nodes of a Node

Lets concentrate on the above picture representing node numbers of array elements. We'll have the following observations

To access the left node of a current node, the node number decreases by 1. If the current node is referred by iCurrent, then its left node can be accessed as:

iLeft=iCurrent-1;		// get node no of the left node

But we have to make sure that this node no. is valid. For this, we have to check:

  • whether this node no. is inside the array bounds
  • whether this node (iLeft) is in the same row of the maze as current node (iCurrent)
  • whether there is a path from current node to this node (to check whether this node is empty or represents a wall etc)

we can do the above by the following code fragment

if (iLeft>=0 && iLeft/iCols==iCurrent/iCols)
    if ( GetNodeContents(m_iMaze, iLeft)==empty )
				    //if left node is not a wall
        // then do something

Similarly, we can write codes for accessing the right node by adding 1 to the current node no.

iRight=iCurrent+1;    // get node no of the right node
if (iRight<iMax && iRight/iCols==iCurrent/iCols)
    if ( GetNodeContents(m_iMaze, iRight)==empty )
        // then do something

for Top and Bottom nodes, we have to add the "no. of columns of the array" to the current node. Also in this case, we only have to check whether the new node no. is inside the bounds of array or not.

iTop=iCurrent-iCols;   // get node no of the top node
if (iTop>=0 )
    if ( GetNodeContents(m_iMaze, iTop)==empty )
				    //if this node is not a wall
        // then do something
iDown=iCurrent+iCols;    // get node no of the bottom node
if (iDown<iMax )
    if ( GetNodeContents(m_iMaze, iDown)==empty )
				    //if this node is not a wall
        // then do something

We can also access 4 diagonals nodes using similar approach. For simplicity of the article, lets only see how our upper-left diagonal is accessed:

if (iLeftUp>=0 && iLeftUp<iMax 
 && iLeftUp/iCols==iCurrent/iCols-1)
    //if upper-left node exists
    if ( GetNodeContents(m_iMaze, iLeftUp)==empty )
				    //if this node is open(a path exists)
        // then do something

Now lets see what is this "do something" in the above code fragments. Once we get the nodes adjacent to the current node, we have to add them to "Queue" if their status is Ready and change their status to Waiting. Lets see how to do this with our left node

iLeft=iCurrent-1;		// get node no of the left node
if (iLeft>=0 && iLeft/iCols==iCurrent/iCols)    
    //if this node exists
    if ( GetNodeContents(m_iMaze, iLeft)==empty )
        //if this node is open(a path exists)
        if (GetNodeContents(iMazeStatus, iLeft) 
         == (int)Status.Ready)
        //if this node is ready
            Queue[iRear]=iLeft; //add to Q
            //change status to waiting
            ChangeNodeContents(iMazeStatus, iLeft, 

Once we have processed all the nodes in this way, we have to do "back-tracking" from our end-point to start-point. The array Queue contains node numbers of all the nodes in the maze which can be reached by node number stored in Origin array at corresponding index. i.e. the array Origin contains node numbers of all such nodes which are used as a path for the corresponding node no. in Queue. For example, lets assume that array contents of Queue and Origin as as under.

Maze Solver

How can we find a path from node no. 1 to 8 :

  • Start with the ending node 8 and see how is it reached. 8 is at 5th index in Queue while the corresponding 5th index in Origin is 9 which implies that 8 can be reached via node 9.
  • Search for value 9 in the Queue. Its at 2nd index while the corresponding element at 2nd index in Origin is 1. This implies that node 9 can be reached via node 1.
  • Search for value 1 in the Queue. Its at the 0th index with corresponding node no. -1 in Origin. This implies that node 1 was our initial point.
  • Hence final path from 1 to 8 is: 1->9->8

Using the class

The class has two constructors, one that takes an integer array directly and the other that takes only dimensions. Indexers can be used to modify the contents of each node (i.e., to build/remove walls/obstacles in the maze).

MazeSolver maze=new MazeSolver(10,12)
maze[2,3]=1    // create wall at postion [2,3]
maze[3,3]=1    // create wall at postion [3,3]
maze[2,3]=0    // remove wall at postion [2,3]

To find a shortest path between, say [1,2] and [6,9], the user should call the method FindPath() which returns a solved maze (of course a 2D integer array) with the path traced by value 100 (this can be changed using the property PathCharacter). If no path exists, the function returns null.

// gets shortest path traced by '100' from [1,2] to [6,9]
int[,] iSolvedMaze=maze.FindPath(1,2,6,9)

About the Demo Project

The application presents a simple way to use the class. It consists of a 16x20 grid where walls can be created or removed. The ball rolls to every new position when the user clicks on a new location accessible from the original location.


Version 1.0

  • Initial version

Version 1.1

  • Extended the demo program so that it can handle rectangular (non-square) mazes also
  • Fixed some small bugs

Version 1.2

  • The class now allows diagonal paths as well.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Syed Mehroz Alam
Software Developer
Pakistan Pakistan

Syed Mehroz Alam, living in Karachi, Pakistan, is a developer focusing Microsoft technologies. He has completed his bachelors as a Computer Systems Engineer in 2006 and is currently pursuing a Masters degree in Computer Science. He loves to learn, discover and master all aspects of .NET and SQL Server. Mehroz has developed rich internet enterprise applications using Silverlight in addition to the traditional ASP.NET and Windows Forms applications. He has worked with all three components of SQL Business Intelligence Studio: SSIS, SSRS and SSAS for developing BI Solutions and Data warehouse. He loves to write complex TSQL queries and evaluate his skills by participating in various TSQL Challenges. His blog can be viewed at

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
vspin6-Jul-12 17:00
membervspin6-Jul-12 17:00 
Generalmodified version - with advanced path analysis Pin
abutun1-Jul-10 8:40
memberabutun1-Jul-10 8:40 
QuestionHow to add path weight Pin
jrcutbu17-Feb-10 13:07
memberjrcutbu17-Feb-10 13:07 
AnswerRe: How to add path weight Pin
Syed Mehroz Alam18-Feb-10 1:01
memberSyed Mehroz Alam18-Feb-10 1:01 
GeneralA small correction i think Pin
Kamran Vighio1-May-09 10:50
memberKamran Vighio1-May-09 10:50 
Generalcheck this out Pin
abutun16-Nov-07 12:37
memberabutun16-Nov-07 12:37 
GeneralShortest path finder component Pin
dark_sidus2-Sep-07 22:07
memberdark_sidus2-Sep-07 22:07 
Generalall paths finder Pin
osazuwa2-Jun-06 8:11
memberosazuwa2-Jun-06 8:11 
GeneralNeeds more to read Pin
Will Gray21-Feb-05 5:05
memberWill Gray21-Feb-05 5:05 
GeneralRe: Needs more to read Pin
Syed Mehroz Alam1-Mar-05 19:57
memberSyed Mehroz Alam1-Mar-05 19:57 
GeneralMaking your code somewhat more efficient Pin
Martin Friedrich16-Feb-05 11:50
memberMartin Friedrich16-Feb-05 11:50 
GeneralRe: Making your code somewhat more efficient Pin
Martin Friedrich16-Feb-05 11:57
memberMartin Friedrich16-Feb-05 11:57 
GeneralRe: Making your code somewhat more efficient Pin
Bert Buiskool25-Feb-05 1:41
sussBert Buiskool25-Feb-05 1:41 
GeneralRe: Making your code somewhat more efficient Pin
David Gallagher1-Mar-05 3:18
memberDavid Gallagher1-Mar-05 3:18 
GeneralRe: Making your code somewhat more efficient Pin
Syed Mehroz Alam1-Mar-05 20:05
memberSyed Mehroz Alam1-Mar-05 20:05 
GeneralRe: Making your code somewhat more efficient Pin
Syed Mehroz Alam3-Mar-05 15:21
memberSyed Mehroz Alam3-Mar-05 15:21 
GeneralRe: Making your code somewhat more efficient Pin
BigBertB8-Mar-05 2:24
memberBigBertB8-Mar-05 2:24 
GeneralRe: Making your code somewhat more efficient Pin
Syed Mehroz Alam1-Mar-05 20:02
memberSyed Mehroz Alam1-Mar-05 20:02 
Generalmaths Pin
surgeproof16-Feb-05 0:28
membersurgeproof16-Feb-05 0:28 
Looks good, but perhaps a little on the mathematics of the functions might be nice. You know, explaining the theory of it and then how to implement it.

'Science without religion is lame, religion without science is blind.' --Albert Einstein
'The pioneers of a warless world are the youth who refuse military service.' --Albert Einstein
GeneralRe: maths Pin
Syed Mehroz Alam17-Feb-05 16:00
memberSyed Mehroz Alam17-Feb-05 16:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170524.1 | Last Updated 1 Mar 2005
Article Copyright 2004 by Syed Mehroz Alam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid