-
Notifications
You must be signed in to change notification settings - Fork 0
/
dailycoding023.py
108 lines (92 loc) · 2.74 KB
/
dailycoding023.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class Point:
def __init__(self, a, b, dist=0):
self.x = a
self.y = b
self.dist = dist
def is_valid(grid, visited, x, y):
"""Check if given point is valid on the grid
- coordinate is within grid
- coordinate has not been visited
- coordinate is not a wall coord
"""
return (0 <= x) and (x < len(grid[0])) and (0 <= y) and (y < len(grid)) \
and grid[x][y] == 0 and not visited[x][y]
def check_window(grid, visited, prev_dist, x, y):
"""iterator for the valid maze points
- checks the left, right, up, down coords
"""
for i, j in ([-1, 0], [0, 1], [1, 0], [0, -1]):
if is_valid(grid, visited, x + i, y + j):
visited[x + i][y + j] = True
yield Point(x + i, y + j, prev_dist + 1)
def shortest_path(grid, start, end):
"""Return length of shortest path using BFS
"""
queue = []
visited = [[False] * len(grid[0]) for _ in range(len(grid))]
# Initialize by pushing start coord to queue
queue.insert(0, start)
visited[start.x][start.y] = True
# Loop until current node has the same coordinates as the end
while queue:
# Get the first node from the queue
node = queue[-1]
queue.pop()
x, y, dist = node.x, node.y, node.dist
if x == end.x and y == end.y:
return dist
# Add all valid neighbors to the queue
for v_node in check_window(grid, visited, dist, x, y):
queue.insert(0, v_node)
# Shortest path not possible
return -1
def main():
"""Shortest path finder using Lee algorithm
"""
mazes = (
[[
[0, 0, 0, 0],
[0, 1, 1, 0],
[0, 1, 0, 0],
[0, 0, 0, 0]
], Point(3, 0), Point(0, 3)],
[[
[0, 0, 0, 0],
[0, 1, 1, 1],
[0, 0, 0, 0],
[0, 0, 0, 0]
], Point(0, 2), Point(3, 2)],
[[
[0, 0, 1, 0],
[1, 0, 0, 1],
[0, 1, 0, 0],
[0, 0, 0, 0]
], Point(3, 0), Point(0, 0)],
[[
[1, 0, 0, 0],
[1, 0, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0]
], Point(3, 0), Point(1, 1)],
[[
[0, 0, 0, 0],
[1, 0, 1, 1],
[0, 1, 0, 0],
[0, 0, 0, 0]
], Point(0, 0), Point(0, 0)],
[[
[0, 0, 0, 0],
[1, 0, 1, 1],
[0, 1, 0, 0],
[0, 0, 0, 0]
], Point(3, 0), Point(0, 0)]
)
maze_paths = (6, 7, 7, 9, 0, -1)
if all([shortest_path(*test) == ans for test, ans in zip(mazes, maze_paths)]):
print("Passed")
else:
print("Failed")
import numpy as np
a = np.
if __name__ == '__main__':
main()