Вы находитесь на странице: 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. Trmaux'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.

2. Underlying Data Structures


Three main data structures were used in creating program.
1.
2.
3.

Stack
1 Dimensional Array
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.

3. Pseudo Code of the program


STRUCTURECell{
INITintegerrow
INITintegercol
}
ALGORITHMMaze{
INITCellcurrentCell,exitCell
INITStackpossibleCells
INITStacktriedCells

SETpossibleCellsnewStackthatcanstorecells
SETtriedCellsnewStackthatcanstorecells

SUBROUTINEbuildMaze(){
SETlengthlengthofmaze
SETwidthwidthofmaze
INITcharmaze[length+2][width+2]

FOR(i=1TOlength+1){
char[]rowgetrowfromuser
FOR(j=1TOwidth+1){
SETmaze[i][j]row[j1]
IF(maze[i][j]='e'){
SETexitCellnewCell(i,j)
}
IF(maze[i][j]='m'){
SETcurrentCellnewCell(i,j)
}
}
}

RETURNmaze
}

SUBROUTINEsolveMaze(char[][]maze){
INITintegeri
INITintegerj
INITbooleansolved
SETsolved<false
do{
CLEARpossibleCells

IF(maze[currentCell.row1][currentCell.col]!='1'AND
maze[currentCell.row1][currentCell.col]!='.'){
possibleCells.push(newCell(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(possibleCellsisNOTEMPTY){
SETmaze[currentCell.row][currentCell.col]'.'
SETcurrentCellpossibleCells.pop()
triedCells.push(currentCell)

IF(maze[currentCell.row][currentCell.col]NOT
EQUAL'e'){
SETmaze[currentCell.row][currentCell.col]'m'
}
}ELSEIF(triedCellsisNOTEMPTY){
SETmaze[currentCell.row][currentCell.col]'.'
SETcurrentCelltriedCells.pop();
SETmaze[currentCell.row][currentCell.col]'m'
}
}WHILE(maze[currentCell.row][currentCell.col]NOTEQUAL'e'))
}
}
4

Вам также может понравиться