Last active
March 30, 2018 13:06
-
-
Save M0r13n/625130d723f69d99e5d4ad4c39d5439b to your computer and use it in GitHub Desktop.
A simple backtracking algorithm for solving a Maze in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OBJECTS = {'WALL': '#', | |
'SPACE': ' ', | |
'VISITED': '.', } | |
def readfile(file): | |
try: | |
f = open(file, 'r') | |
except Exception as e: | |
print('Error opening file. Did you enter the correct file-path?') | |
print(e) | |
# read each line | |
grid = [line.replace('\n', '').split(',') for line in f] | |
return grid | |
def check_matrix(grid): | |
for i in range(0, len(grid)): | |
for j in range(0, len(grid[i])): | |
# if there is an undefined character, something is most likely going to be wrong | |
if not grid[i][j] in OBJECTS.values(): | |
return False | |
return True | |
def valid(x, y, grid): | |
# to be valid the position has to be inside the grid and not yet been visited | |
return 0 <= x < len(grid[0]) and 0 <= y < len(grid) and grid[y][x] is OBJECTS['SPACE'] | |
def find_path(grid, x, y, endx, endy, path): | |
# destination reached ? | |
if x == endx and y == endy: | |
return path | |
# current position must be valid | |
if not valid(x, y, grid): | |
return None | |
# mark current position | |
grid[y][x] = OBJECTS['VISITED'] | |
# try every possible direction | |
# north | |
if find_path(grid, x, y - 1, endx, endy, path) is not None: | |
path.append('N') | |
return path | |
# south | |
if find_path(grid, x, y + 1, endx, endy, path) is not None: | |
path.append('S') | |
return path | |
# east | |
if find_path(grid, x + 1, y, endx, endy, path) is not None: | |
path.append('E') | |
return path | |
# west | |
if find_path(grid, x - 1, y + 1, endx, endy, path) is not None: | |
path.append('W') | |
return path | |
# every possible step was invalid | |
# so just take a step back | |
if len(path) > 0: | |
path.remove(len(path)-1) | |
grid[y][x] = OBJECTS['SPACE'] | |
grid = readfile('test1.txt') | |
check_matrix(grid) | |
path = find_path(grid, 1, 1, 3, 3, []) | |
if path is None: | |
path = [] | |
print(path[len(path)-1:0:-1]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#,#,#,#,#,#,#,#,#,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , ,#, , , ,# | |
#, , , , , , , , ,# | |
#,#,#,#,#,#,#,#,#,# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
,#,#,#, | |
, ,#, , | |
#, ,#, , | |
, , , , |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment