0% found this document useful (0 votes)
319 views

Maze Solving Algorithm

This document contains the pseudocode for a maze solving algorithm based on a random mouse approach. It uses three main data structures: a stack to store possible paths, and one-dimensional and two-dimensional arrays to represent the maze. The algorithm starts at the starting cell and checks all neighboring empty cells, adding them to a possible cells stack. It pops the last cell from the stack and makes it the current cell, marking the path. If a dead end is reached, it backtracks until a new possible path is found. This continues until the exit cell is reached.

Uploaded by

Isuru Buddika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
319 views

Maze Solving Algorithm

This document contains the pseudocode for a maze solving algorithm based on a random mouse approach. It uses three main data structures: a stack to store possible paths, and one-dimensional and two-dimensional arrays to represent the maze. The algorithm starts at the starting cell and checks all neighboring empty cells, adding them to a possible cells stack. It pops the last cell from the stack and makes it the current cell, marking the path. If a dead end is reached, it backtracks until a new possible path is found. This continues until the exit cell is reached.

Uploaded by

Isuru Buddika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Table of Contents

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

1. Wall follower (Left / Right)


2. Trémaux's algorithm
3. Random mouse algorithm
4. Dead end filling

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.

The process of solving the maze in this program is done as followes.


From the starting position all the possible directions(cells) are checked and kept in memory
so that they are accesible in the order last to first. Cheking is done in the order upper, lower, left,
right. Then the path is advanced in the last possible direction(right). This is done until a dead end is
met. At a dead end path is taken back one cell at a time and above process is repeated at each
cell. If possible cell is found path is extended in that way.
In this way maze is solved by checking out possible paths systematically unti the exit is met or all
the paths are checked.

1
2. Underlying Data Structures

Three main data structures were used in creating program.

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

SET possibleCells ← new Stack that can store cells


SET triedCells ← new Stack that can store cells

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[j­1]
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.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+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.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+1]!='1' AND
maze[currentCell.row][currentCell.col+1]!='.'){
possibleCells.push(new
Cell(currentCell.row,currentCell.col+1))
}

IF(possibleCells is NOT EMPTY){


SET maze[currentCell.row][currentCell.col] ← '.'
SET currentCell ← possibleCells.pop()
triedCells.push(currentCell)

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'))
}
}

You might also like