-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q02_robot_in_a_grid.py
72 lines (61 loc) · 1.73 KB
/
Q02_robot_in_a_grid.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
import unittest
from collections import deque
def recreate_path(start, end, previous_position):
pos = start
path = []
while pos != end:
if pos not in previous_position:
return []
path.append(pos)
pos = previous_position[pos]
return path + [end]
def get_path(grid):
h, w = len(grid), len(grid[0])
previous_position = {}
queue = deque([(h - 1, w - 1)])
directions = [(-1, 0), (0, -1)]
while queue:
pos = queue.popleft()
for xr, xc in directions:
new_r, new_c = pos[0] + xr, pos[1] + xc
if (
0 <= new_r < h
and 0 <= new_c < w
and grid[new_r][new_c] == 0
and (new_r, new_c) not in previous_position
):
previous_position[(new_r, new_c)] = pos
queue.append((new_r, new_c))
return recreate_path((0, 0), (h - 1, w - 1), previous_position)
class Test(unittest.TestCase):
def test_path_through_grid(self):
grid = [
[0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 0, 1, 1, 0],
[0, 0, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 1, 0],
]
self.assertEqual(
get_path(grid),
[
(0, 0),
(0, 1),
(0, 2),
(0, 3),
(1, 3),
(2, 3),
(2, 4),
(2, 5),
(2, 6),
(3, 6),
],
)
grid = [
[0, 1, 0, 0, 0, 0, 1],
[1, 1, 1, 0, 1, 1, 0],
[0, 0, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 1, 0],
]
self.assertEqual(
get_path(grid), [],
)