-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcan_exit.py
62 lines (52 loc) · 1.41 KB
/
can_exit.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#Input: Maze denoted with grid nxm
#0 denotes walkable path, 1 denotes wall
"""
can_exit([
[0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 1, 1],
[1, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 0, 0]
]) -> True
"""
def can_exit(lst):
maze = lst
#Denote goal = 5
maze[-1][-1] = 5
return search(0,0, maze)
def search(row, column, maze):
print("Scanning", row, column)
#Get a visual of where I am, where 2 = location
state = maze[row][column]
maze[row][column] = 2
for i in maze:
print(i)
#Change the 2 (position) back to what it was before
maze[row][column] = state
#False where we visited and walls
#Only allow true if exit is found
if maze[row][column] == 5:
print("Found exit at: ", row, column)
return True
elif maze[row][column] == 1:
print("Hit wall at ", row, column)
return False
elif maze[row][column] == 3:
print("Visited", row, column)
return False
#Mark area visited
maze[row][column] = 3
#Look right, then down, then left, then up
if ((row < len(maze) - 1 and search(row + 1, column, maze))
or (column > 0 and search(row, column - 1, maze))
or (row > 0 and search(row - 1, column, maze))
or (column < len(maze[0]) - 1 and search(row, column + 1, maze))):
return True
return False
can_exit([
[0, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 1, 0, 1, 1],
[1, 0, 0, 0, 0, 1, 1],
[1, 1, 1, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 0, 0]
])