I have written a program that solves a maze recursively. It opens a text file containing a maze, converts it into a list of lists, and then try to recursively solve it. Here's the part that solves the maze:
def search(x,y, mazeList):
# returns True if it has found end of maze
if mazeList[x][y] == 'E':
return True
# returns False if it encounters a wall
elif mazeList[x][y] == '-':
return False
elif mazeList[x][y] == '+':
return False
elif mazeList[x][y] == "|":
return False
# returns False if it finds a visited path
elif mazeList[x][y] == '*':
return False
# marks path with '*'
mazeList[x][y] = '*'
# recursive search
if ((search(x+1, y, mazeList))
or (search(x, y-1, mazeList))
or (search(x-1, y, mazeList))
or (search(x, y+1, mazeList))):
return True
return False
In the maze, '-', '+' and '|' make up the walls of the maze, empty spaces can be navigated and 'E' is the end of maze. It starts from lower left part of the maze, and goes from there. I want the correct path to be marked with *, however it marks every path it takes with * even if it's the wrong path from which it backtracks.
So how can I edit my code so that in the end, only the correct path from start to finish is marked with *