Skip to content

Commit 8f14400

Browse files
committed
test: adding more tests to a star algorithm
1 parent d96029e commit 8f14400

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

graphs/a_star.py

+24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ def search(
1616
cost: int,
1717
heuristic: list[list[int]],
1818
) -> 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+
"""
1943
closed = [
2044
[0 for col in range(len(grid[0]))] for row in range(len(grid))
2145
] # the reference grid

0 commit comments

Comments
 (0)