-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathp8_2.py
50 lines (38 loc) · 1.03 KB
/
p8_2.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
from typing import List
def robot_br(m: List[List[int]], r, c) -> List:
rm = [[None for _ in range(c)] for _ in range(r)]
def up_left_rec(i, j, dir):
if 0 > i or j < 0:
return
if rm[i][j] is None:
rm[i][j] = dir
up_left_rec(i-1, j, "u")
up_left_rec(i, j-1, "l")
for i in range(r):
for j in range(c):
if m[i][j]:
rm[i][j] = "b"
up_left_rec(r-1, c-1, "e")
if not rm[0][0]:
return []
path = [(0, 0)]
i, j = 0, 0
while rm[i][j] != "e":
dir = rm[i][j]
if dir == "u":
i += 1
elif dir == "l":
j += 1
path.append((i, j))
for line in rm:
print(line)
return path
if __name__ == "__main__":
ex1 = [[0, 1, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 0, 0, 0],
]
r, c = len(ex1), len(ex1[0])
print(f"For the input ,result path is {robot_br(ex1, r, c)}")