maze traversal

#!/usr/bin/env python
#
# Naive recursive search for path traversal in a simple maze.
#
# Usage:
#
# $ python maze.py 
#   S # # # # #
#   . . . . . #
#   # . # # # #
#   # . # # # #
#   . . . # . G
#   # # . . . #
#
#   S # # # # #
#   o o . . . #
#   # o # # # #
#   # o # # # #
#   x o o # o G
#   # # o o o #

maze = [['S','#','#','#','#','#'],
        ['.','.','.','.','.','#'],
        ['#','.','#','#','#','#'],
        ['#','.','#','#','#','#'],
        ['.','.','.','#','.','G'],
        ['#','#','.','.','.','#']]

def find_path(x, y):
    """ Finds a path through our character-based maze (stored in a 2D list).
    Uses recursion. See www.cs.bu.edu/teaching/alg/maze for more details.
    """

    if x < 0 or y < 0 or x > 5 or y > 5: return False  # If outside maze.
    if maze[x][y] == 'G': return True  # If found goal.
    if maze[x][y] == '#' or maze[x][y] == 'o': return False  # If position not open.

    maze[x][y] = 'o'  # Mark (x,y) as part of solution path.
    
    if find_path(x, y-1): return True  # Search north.
    if find_path(x+1, y): return True  # Search east.
    if find_path(x, y+1): return True  # Search south.
    if find_path(x-1, y): return True  # Search west.

    maze[x][y] = 'x'  # Unmark (x,y) as part of solution, was a dead-end.

    return False

def print_maze_raw():
    for char in maze:
        print char
    print

def print_maze():
    for i in range(len(maze)):
        for j in range(len(maze[0])):
            print(maze[i][j]),
        print
    print

if __name__ == '__main__':
    print_maze()  # Before.
    find_path(0,0)  # Starting position is (0,0).
    maze[0][0] = 'S'  # Remark our starting position (got lost during path traversal).
    print_maze()  # After.

Hot pastes

free anonymous socks5 proxies
     30472 | Never
maze traversal
     2729 | Never
hello, world
     2321 | Never
bitwise operations
     1629 | Never
free anonymous http proxies
     1043 | 2022-07-12
Office 2021 activation.bat
     893 | 2022-12-12
DEVISIB All-in-One Coffee Machine Espresso Coffee Maker with Grinder Automatic coffee makers
     599 | 2022-12-04
leetcode 621 task scheduler python
     269 | Never
log4j search
     185 | Never
Fresh Cc Cvc Fullz PayPal Transfer WU Transfer Bug ACH Wire Cashapp Skrill Zelle Visa Debit Msr Ship shop Administration
     160 | 2022-01-12