1
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 little bit, and that was the result(It cost me several months), now, I’m applying this same code in Astar (which is almost costing me a year), to find the minimum path, But I still don’t understand one thing, how does the Solver() function perform several times? because I don’t see any repeating structure. It would be the "working together" of the boolean variable found with the condition if __name__ == "__main__"?
site where I found the old code: [https://wiki.python.org.br/ResolvedorLabirinto]
Recursiveness. It is called the function
solve
within itself.– Woss
Thank you very much! I will research more on the subject.
– Marcos Vinícius