@@ -16,6 +16,30 @@ def search(
16
16
cost : int ,
17
17
heuristic : list [list [int ]],
18
18
) -> tuple [list [list [int ]], list [list [int ]]]:
19
+ """
20
+ Search for a path on a grid avoiding obstacles.
21
+ >>> grid = [[0, 1, 0, 0, 0, 0], \
22
+ [0, 1, 0, 0, 0, 0], \
23
+ [0, 1, 0, 0, 0, 0], \
24
+ [0, 1, 0, 0, 1, 0], \
25
+ [0, 0, 0, 0, 1, 0]]
26
+ >>> init = [0, 0]
27
+ >>> goal = [len(grid)-1, len(grid[0])-1]
28
+ >>> cost = 1
29
+ >>> heuristic = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
30
+ >>> for i in range(len(grid)):
31
+ ... for j in range(len(grid[0])):
32
+ ... heuristic[i][j] = abs(i - goal[0]) + abs(j - goal[1])
33
+ ... if grid[i][j] == 1:
34
+ ... heuristic[i][j] = 99
35
+ >>> path, action = search(grid, init, goal, cost, heuristic)
36
+ >>> print(path)
37
+ [[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [4, 1], [4, 2],\
38
+ [4, 3], [3, 3], [2, 3], [2, 4], [2, 5], [3, 5], [4, 5]]
39
+ >>> print(action)
40
+ [[0, 0, 0, 0, 0, 0], [2, 0, 0, 0, 0, 0], [2, 0, 0, 0, 3, 3],\
41
+ [2, 0, 0, 0, 0, 2], [2, 3, 3, 3, 0, 2]]
42
+ """
19
43
closed = [
20
44
[0 for col in range (len (grid [0 ]))] for row in range (len (grid ))
21
45
] # the reference grid
0 commit comments