-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze_checker.py
68 lines (54 loc) · 1.87 KB
/
maze_checker.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
63
64
65
66
67
68
from stack_array import Stack
def is_solvable(the_maze):
search_locations = Stack()
search_locations.push(find_start(the_maze))
while not search_locations.is_empty():
#print_maze(the_maze)
#input()
row, col = search_locations.pop()
if the_maze[row][col] == "!":
print_maze(the_maze)
return True
the_maze[row][col] = "X"
# check left
process_coordinate(the_maze,row,col-1,search_locations)
# check down
process_coordinate(the_maze,row+1,col,search_locations)
# check right
process_coordinate(the_maze,row,col+1,search_locations)
# check up
process_coordinate(the_maze,row-1,col,search_locations)
print_maze(the_maze)
return False
def process_coordinate(the_maze,row,col,search_locations):
if 0 <= row < len(the_maze):
if 0 <= col < len(the_maze[row]):
if the_maze[row][col] == " ":
the_maze[row][col] = "?"
search_locations.push((row,col))
if the_maze[row][col] == "F":
the_maze[row][col] = "!"
search_locations.push((row,col))
def read_maze(filepath):
fin = open(filepath)
maze = []
for line in fin:
maze_row = []
# could use rstrip("\n")
for c in line.rstrip():
maze_row.append(c)
maze.append(maze_row)
fin.close()
return maze
def print_maze(the_maze):
for maze_row in the_maze:
print("".join(maze_row))
def find_start(the_maze):
for row_index in range(len(the_maze)):
for col_index in range(len(the_maze[row_index])):
if the_maze[row_index][col_index] == "S":
return row_index, col_index
if __name__ == "__main__":
the_maze = read_maze("maze.txt")
print_maze(the_maze)
print(is_solvable(the_maze))