7
7
8
8
9
9
class Cell :
10
- def __init__ (self )-> None :
11
- # Parent cell's row index
10
+ def __init__ (self ) -> None :
11
+ # Parent cell's row index
12
12
self .parent_i = 0
13
13
# Parent cell's column index
14
14
self .parent_j = 0
@@ -28,34 +28,134 @@ def __init__(self)->None:
28
28
29
29
30
30
def is_valid (row : int , col : int ) -> bool :
31
+ """
32
+ Check if a cell is within the grid bounds.
33
+
34
+ Args:
35
+ row (int): The row index of the cell.
36
+ col (int): The column index of the cell.
37
+
38
+ Returns:
39
+ bool: True if the cell is within bounds, False otherwise.
40
+
41
+ Examples:
42
+ >>> is_valid(5, 3)
43
+ True
44
+ >>> is_valid(9, 5)
45
+ False
46
+ >>> is_valid(-1, 0)
47
+ False
48
+ """
31
49
return (row >= 0 ) and (row < ROW ) and (col >= 0 ) and (col < COL )
32
50
33
51
34
52
# Check if a cell is unblocked
35
53
36
54
37
55
def is_unblocked (grid : List [List [int ]], row : int , col : int ) -> bool :
56
+ """
57
+ Check if a cell is unblocked in the grid.
58
+
59
+ Args:
60
+ grid (List[List[int]]): The grid representing the map.
61
+ row (int): The row index of the cell.
62
+ col (int): The column index of the cell.
63
+
64
+ Returns:
65
+ bool: True if the cell is unblocked (1), False if blocked (0).
66
+
67
+ Examples:
68
+ >>> grid = [
69
+ ... [1, 0, 1],
70
+ ... [1, 1, 0],
71
+ ... [0, 1, 1]
72
+ ... ]
73
+ >>> is_unblocked(grid, 0, 0)
74
+ True
75
+ >>> is_unblocked(grid, 0, 1)
76
+ False
77
+ >>> is_unblocked(grid, 2, 2)
78
+ True
79
+ """
38
80
return grid [row ][col ] == 1
39
81
40
82
41
83
# Check if a cell is the destination
42
84
43
85
44
86
def is_destination (row : int , col : int , dest : Tuple [int , int ]) -> bool :
87
+ """
88
+ Check if a cell is the destination.
89
+
90
+ Args:
91
+ row (int): The row index of the cell.
92
+ col (int): The column index of the cell.
93
+ dest (Tuple[int, int]): The destination coordinates as (row, col).
94
+
95
+ Returns:
96
+ bool: True if the cell is the destination, False otherwise.
97
+
98
+ Examples:
99
+ >>> is_destination(3, 4, (3, 4))
100
+ True
101
+ >>> is_destination(2, 1, (3, 4))
102
+ False
103
+ """
45
104
return row == dest [0 ] and col == dest [1 ]
46
105
47
106
48
107
# Calculate the heuristic value of a cell (Euclidean distance to destination)
49
108
50
109
51
110
def calculate_h_value (row : int , col : int , dest : Tuple [int , int ]) -> float :
111
+ """
112
+ Calculate the heuristic value (Euclidean distance) from a cell to the destination.
113
+
114
+ Args:
115
+ row (int): The row index of the cell.
116
+ col (int): The column index of the cell.
117
+ dest (Tuple[int, int]): The destination coordinates as (row, col).
118
+
119
+ Returns:
120
+ float: The Euclidean distance from the current cell to the destination.
121
+
122
+ Examples:
123
+ >>> calculate_h_value(0, 0, (3, 4))
124
+ 5.0
125
+ >>> calculate_h_value(2, 1, (2, 1))
126
+ 0.0
127
+ >>> calculate_h_value(1, 1, (4, 5))
128
+ 5.0
129
+ """
52
130
return ((row - dest [0 ]) ** 2 + (col - dest [1 ]) ** 2 ) ** 0.5
53
131
54
132
55
133
# Trace the path from source to destination
56
134
57
135
58
136
def trace_path (cell_details : List [List [Cell ]], dest : Tuple [int , int ]) -> None :
137
+ """
138
+ Trace and print the path from the source to the destination.
139
+
140
+ Args:
141
+ cell_details (List[List[Cell]]): A 2D list containing details of each cell.
142
+ dest (Tuple[int, int]): The destination coordinates as (row, col).
143
+
144
+ Returns:
145
+ None
146
+
147
+ Examples:
148
+ >>> class Cell:
149
+ ... def __init__(self):
150
+ ... self.parent_i = 0
151
+ ... self.parent_j = 0
152
+ >>> cell_details = [[Cell() for _ in range(2)] for _ in range(2)]
153
+ >>> cell_details[1][1].parent_i = 0
154
+ >>> cell_details[1][1].parent_j = 0
155
+ >>> trace_path(cell_details, (1, 1))
156
+ The Path is
157
+ -> (0, 0) -> (1, 1)
158
+ """
59
159
print ("The Path is " )
60
160
path = []
61
161
row = dest [0 ]
@@ -87,6 +187,30 @@ def trace_path(cell_details: List[List[Cell]], dest: Tuple[int, int]) -> None:
87
187
88
188
89
189
def a_star_search (grid : List [List [int ]], src : Tuple [int , int ], dest : Tuple [int , int ]) -> None :
190
+ """
191
+ Perform the A* search to find the shortest path from source to destination.
192
+
193
+ Args:
194
+ grid (List[List[int]]): The grid representing the map, where 1 is unblocked and 0 is blocked.
195
+ src (Tuple[int, int]): The source coordinates as (row, col).
196
+ dest (Tuple[int, int]): The destination coordinates as (row, col).
197
+
198
+ Returns:
199
+ None
200
+
201
+ Examples:
202
+ >>> grid = [
203
+ ... [1, 1, 1],
204
+ ... [1, 0, 1],
205
+ ... [1, 1, 1]
206
+ ... ]
207
+ >>> a_star_search(grid, (0, 0), (2, 2))
208
+ The destination cell is found
209
+ The Path is
210
+ -> (0, 0) -> (1, 1) -> (2, 2)
211
+ >>> a_star_search(grid, (0, 0), (1, 1))
212
+ Source or the destination is blocked
213
+ """
90
214
# Check if the source and destination are valid
91
215
if not is_valid (src [0 ], src [1 ]) or not is_valid (dest [0 ], dest [1 ]):
92
216
print ("Source or destination is invalid" )
@@ -203,8 +327,8 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
203
327
Examples:
204
328
>>> main()
205
329
The destination cell is found
206
- The Path is
207
- -> (8, 0) -> (7, 1) -> (6, 0) -> (5, 1) -> (4, 0) -> (3, 1) -> (2, 0) -> (1, 1) -> (0, 0)
330
+ The Path is
331
+ -> (8, 0) -> (7, 1) -> (6, 0) -> (5, 1) -> (4, 0) -> (3, 1) -> (2, 0) -> (1, 1) -> (0, 0)
208
332
"""
209
333
# Define the grid (1 for unblocked, 0 for blocked)
210
334
grid = [
0 commit comments