Search a maze - alternative python solution

#1

My solution for the maze problem. It’s a few lines shorter and might be clearer for some people. Hope it will help someone!

def is_legal_move(maze, position):
    return (0 <= position.x < len(maze) and 0 <= position.y < len(maze) and maze[position.y][position.x] == 0)

def search_maze(maze, current, end, path=[]):
    path = path + [current]
    if current == end:
        return path
    for next_move in (Coordinate(current.x - 1, current.y), Coordinate(current.x + 1, current.y), Coordinate(current.x, current.y + 1), Coordinate(current.x, current.y - 1)):
        if next_move not in path and is_legal_move(maze,current):
            tested_path = search_maze(maze, next_move, end, path)
            if tested_path is not None:
                return tested_path
    return None
1 Like

#2

Hi @inoam,

Much appreciated for your input here and it looks good to me!

1 Like