1
1
# Python program for A* Search Algorithm
2
2
import math
3
3
import heapq
4
+ from typing import List , Tuple
4
5
5
6
# Define the Cell class
6
7
7
8
8
9
class Cell :
9
- def __init__ (self ):
10
+ def __init__ (self )-> None :
10
11
# Parent cell's row index
11
12
self .parent_i = 0
12
13
# Parent cell's column index
@@ -26,31 +27,31 @@ def __init__(self):
26
27
# Check if a cell is valid (within the grid)
27
28
28
29
29
- def is_valid (row , col ) :
30
+ def is_valid (row : int , col : int ) -> bool :
30
31
return (row >= 0 ) and (row < ROW ) and (col >= 0 ) and (col < COL )
31
32
32
33
# Check if a cell is unblocked
33
34
34
35
35
- def is_unblocked (grid , row , col ) :
36
+ def is_unblocked (grid : List [ List [ int ]] , row : int , col : int ) -> bool :
36
37
return grid [row ][col ] == 1
37
38
38
39
# Check if a cell is the destination
39
40
40
41
41
- def is_destination (row , col , dest ) :
42
+ def is_destination (row : int , col : int , dest : Tuple [ int , int ]) -> bool :
42
43
return row == dest [0 ] and col == dest [1 ]
43
44
44
45
# Calculate the heuristic value of a cell (Euclidean distance to destination)
45
46
46
47
47
- def calculate_h_value (row , col , dest ) :
48
+ def calculate_h_value (row : int , col : int , dest : Tuple [ int , int ]) -> float :
48
49
return ((row - dest [0 ]) ** 2 + (col - dest [1 ]) ** 2 ) ** 0.5
49
50
50
51
# Trace the path from source to destination
51
52
52
53
53
- def trace_path (cell_details , dest ) :
54
+ def trace_path (cell_details : List [ List [ Cell ]] , dest : Tuple [ int , int ]) -> None :
54
55
print ("The Path is " )
55
56
path = []
56
57
row = dest [0 ]
@@ -77,7 +78,7 @@ def trace_path(cell_details, dest):
77
78
# Implement the A* search algorithm
78
79
79
80
80
- def a_star_search (grid , src , dest ) :
81
+ def a_star_search (grid : List [ List [ int ]] , src : Tuple [ int , int ], dest : Tuple [ int , int ]) -> None :
81
82
# Check if the source and destination are valid
82
83
if not is_valid (src [0 ], src [1 ]) or not is_valid (dest [0 ], dest [1 ]):
83
84
print ("Source or destination is invalid" )
@@ -167,7 +168,19 @@ def a_star_search(grid, src, dest):
167
168
# Driver Code
168
169
169
170
170
- def main ():
171
+ def main () -> None :
172
+ """
173
+ Run the A* search algorithm on a predefined grid.
174
+
175
+ Returns:
176
+ None
177
+
178
+ Examples:
179
+ >>> main()
180
+ The destination cell is found
181
+ The Path is
182
+ -> (8, 0) -> (7, 1) -> (6, 0) -> (5, 1) -> (4, 0) -> (3, 1) -> (2, 0) -> (1, 1) -> (0, 0)
183
+ """
171
184
# Define the grid (1 for unblocked, 0 for blocked)
172
185
grid = [
173
186
[1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ],
@@ -182,12 +195,12 @@ def main():
182
195
]
183
196
184
197
# Define the source and destination
185
- src = [ 8 , 0 ]
186
- dest = [ 0 , 0 ]
198
+ src = ( 8 , 0 )
199
+ dest = ( 0 , 0 )
187
200
188
201
# Run the A* search algorithm
189
202
a_star_search (grid , src , dest )
190
203
191
-
192
204
if __name__ == "__main__" :
193
205
main ()
206
+
0 commit comments