-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_pathfinder.py
43 lines (35 loc) · 1.31 KB
/
maze_pathfinder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
################################
# Author: Asher Minden-Webb #
# Date: February 8, 2016 #
################################
def is_path(grid):
"""
Returns true if there is a path from grid[0][0] to a cell containing 2.
Cells containing 1 are paths and cells containing 0 are walls. Paths
may only move vertically and horizontally, not diagonally.
@param grid: nxm 2-dimensional list representing a maze
@rtype: boolean
"""
if len(grid) == 0 or len(grid[0]) == 0:
return False
# Call to recursive helper function, made with *copy* of grid
return _is_path(grid[:], 0, 0)
def _is_path(grid, x, y):
"""
Helper function for is_path(). "grid" parameter will be modified.
@param grid: nxm 2-dimensional list, modifiable
@param x: x-coordinate to search
@param y: y-coordinate to search
@rtype boolean
"""
# Recursive breadth-first search, from top-left to bottom-right of grid.
# Visited cells are marked with -1
if grid[y][x] == 2:
grid[y][x] = -1
return True
if grid[y][x] in [0, -1]:
grid[y][x] = -1
return False
return \
(_is_path(grid, x, y + 1) if len(grid) > y + 1 else False) or \
(_is_path(grid, x + 1, y) if len(grid[0]) > x + 1 else False)