maze = """\
##############
# #
# #
# ##########
# # X#
# ## #### #
# # ####
# #
##############
"""
EMPTY, BLOCK, STEP, END ,WALL=" ", "*", ".", "X", "#"
UP, DOWN, LEFT, RIGHT = "˄", "˅", "<", ">"
north, south, east, west=0, 1, 2, 3
movem = {
north: (lambda x,y:(x, y-1)),
south: (lambda x,y:(x, y+1)),
east : (lambda x,y:(x-1, y)),
west : (lambda x,y:(x+1, y))
}
def solve(maze, x, y, move):
found = False
if 0 <= x < len(maze[0]) and 0 <= y < len(maze):
if maze[y][x] == EMPTY:
maze[y][x] = BLOCK
if (solve(maze, x+1, y, RIGHT) or solve(maze, x, y+1, DOWN) or
solve(maze, x-1, y, LEFT) or solve(maze, x, y-1, UP)):
maze[y][x] = move
found = True
elif maze[y][x] == END:
found = True
return found
if __name__ == "__main__":
maze = [[char for char in line] for line in maze.splitlines()]
solve(maze, 1, 4, EMPTY)
for line in maze:
print("".join(line))
I saw this code on the internet in an older version, and I decided to improve it a bit, and that was the result (this cost me several months), now I'm applying that same code in AStar (which is almost costing me a year) to find the minimum path, but I still do not understand one thing, how does the solver () function execute multiple times ?, since I do not see any repetition structure. It would be the "working together" of the boolean found condition with the condition if __name__ == "__main __" ?
site where I found the old code: [ link