Maze Solving Algorithm
Maze Solving Algorithm
Topic Page
1. Introduction................................................................................................................... 01
2. Underlying data structures............................................................................................ 02
3. Pseudo code of the program......................................................................................... 03-04
1. Introduction
There are several algorithms used in solving mazes using computers. Some algorithms make use
of actulal scenario used by humans when entrapped in a maze while others used methods that can
be used only in computers. Some of those maze solving algorithms are
The algorithm used in this program is related to random mouse algorithm. In the random mouse
algorithm solving is done by proceeding in a straight line until a junction is met and at the junction
making a random decision on the side to turn or go straight.
1
2. Underlying Data Structures
1. Stack
2. 1 Dimensional Array
3. 2 Dimensional Array
A special structures “Cell” was implemented and used in order to keep the x and y coordinates of
the relevant point in the maze. So that the maze is implemented as a collection of cells.
Stack was not implemented as pre implemented stack structure can be found in java collections.
2
3. Pseudo Code of the program
STRUCTURE Cell{
INIT integer row
INIT integer col
}
ALGORITHM Maze{
INIT Cell currentCell, exitCell
INIT Stack possibleCells
INIT Stack triedCells
SUBROUTINE buildMaze(){
SET length ← length of maze
SET width ← width of maze
INIT char maze[length+2][width+2]
FOR(i=1 TO length+1){
char[] row ← get row from user
FOR(j=1 TO width+1){
SET maze[i][j] ← row[j1]
IF(maze[i][j]='e'){
SET exitCell ← new Cell(i,j)
}
IF(maze[i][j]='m'){
SET currentCell ← new Cell(i,j)
}
}
}
RETURN maze
}
3
SUBROUTINE solveMaze(char[][] maze){
INIT integer i
INIT integer j
INIT boolean solved
SET solved < false
do{
CLEAR possibleCells
IF(maze[currentCell.row1][currentCell.col]!='1' AND
maze[currentCell.row1][currentCell.col]!='.'){
possibleCells.push(new Cell(currentCell.row
1,currentCell.col))
}
IF(maze[currentCell.row+1][currentCell.col]!='1' AND
maze[currentCell.row+1][currentCell.col]!='.'){
possibleCells.push(new
Cell(currentCell.row+1,currentCell.col))
}
IF(maze[currentCell.row][currentCell.col1]!='1' AND
maze[currentCell.row][currentCell.col1]!='.'){
possibleCells.push(new
Cell(currentCell.row,currentCell.col1))
}
IF(maze[currentCell.row][currentCell.col+1]!='1' AND
maze[currentCell.row][currentCell.col+1]!='.'){
possibleCells.push(new
Cell(currentCell.row,currentCell.col+1))
}
IF(maze[currentCell.row][currentCell.col] NOT
EQUAL'e'){
SET maze[currentCell.row][currentCell.col] ← 'm'
}
}ELSE IF(triedCells is NOT EMPTY){
SET maze[currentCell.row][currentCell.col] ← '.'
SET currentCell ← triedCells.pop();
SET maze[currentCell.row][currentCell.col] ← 'm'
}
}WHILE(maze[currentCell.row][currentCell.col] NOT EQUAL 'e'))
}
}