7
7
8
8
9
9
class Cell :
10
+ < << << << HEAD
10
11
def __init__ (self )-> None :
11
12
# Parent cell's row index
13
+ == == == =
14
+ def __init__ (self ):
15
+ # Parent cell's row index
16
+ >> >> >> > 04 d1c193f3be0847c2a2d643aff6107aa52146ff
12
17
self .parent_i = 0
13
- # Parent cell's column index
18
+ # Parent cell's column index
14
19
self .parent_j = 0
15
- # Total cost of the cell (g + h)
16
- self .f = float (' inf' )
17
- # Cost from start to this cell
18
- self .g = float (' inf' )
19
- # Heuristic cost from this cell to destination
20
+ # Total cost of the cell (g + h)
21
+ self .f = float (" inf" )
22
+ # Cost from start to this cell
23
+ self .g = float (" inf" )
24
+ # Heuristic cost from this cell to destination
20
25
self .h = 0
21
26
22
27
@@ -30,24 +35,28 @@ def __init__(self)->None:
30
35
def is_valid (row : int , col : int ) -> bool :
31
36
return (row >= 0 ) and (row < ROW ) and (col >= 0 ) and (col < COL )
32
37
38
+
33
39
# Check if a cell is unblocked
34
40
35
41
36
42
def is_unblocked (grid : List [List [int ]], row : int , col : int ) -> bool :
37
43
return grid [row ][col ] == 1
38
44
45
+
39
46
# Check if a cell is the destination
40
47
41
48
42
49
def is_destination (row : int , col : int , dest : Tuple [int , int ]) -> bool :
43
50
return row == dest [0 ] and col == dest [1 ]
44
51
52
+
45
53
# Calculate the heuristic value of a cell (Euclidean distance to destination)
46
54
47
55
48
56
def calculate_h_value (row : int , col : int , dest : Tuple [int , int ]) -> float :
49
57
return ((row - dest [0 ]) ** 2 + (col - dest [1 ]) ** 2 ) ** 0.5
50
58
59
+
51
60
# Trace the path from source to destination
52
61
53
62
@@ -58,7 +67,10 @@ def trace_path(cell_details: List[List[Cell]], dest: Tuple[int, int]) -> None:
58
67
col = dest [1 ]
59
68
60
69
# Trace the path from destination to source using parent cells
61
- while not (cell_details [row ][col ].parent_i == row and cell_details [row ][col ].parent_j == col ):
70
+ while not (
71
+ cell_details [row ][col ].parent_i == row
72
+ and cell_details [row ][col ].parent_j == col
73
+ ):
62
74
path .append ((row , col ))
63
75
temp_row = cell_details [row ][col ].parent_i
64
76
temp_col = cell_details [row ][col ].parent_j
@@ -75,6 +87,7 @@ def trace_path(cell_details: List[List[Cell]], dest: Tuple[int, int]) -> None:
75
87
print ("->" , i , end = " " )
76
88
print ()
77
89
90
+
78
91
# Implement the A* search algorithm
79
92
80
93
@@ -85,7 +98,9 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
85
98
return
86
99
87
100
# Check if the source and destination are unblocked
88
- if not is_unblocked (grid , src [0 ], src [1 ]) or not is_unblocked (grid , dest [0 ], dest [1 ]):
101
+ if not is_unblocked (grid , src [0 ], src [1 ]) or not is_unblocked (
102
+ grid , dest [0 ], dest [1 ]
103
+ ):
89
104
print ("Source or the destination is blocked" )
90
105
return
91
106
@@ -126,14 +141,26 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
126
141
closed_list [i ][j ] = True
127
142
128
143
# For each direction, check the successors
129
- directions = [(0 , 1 ), (0 , - 1 ), (1 , 0 ), (- 1 , 0 ),
130
- (1 , 1 ), (1 , - 1 ), (- 1 , 1 ), (- 1 , - 1 )]
144
+ directions = [
145
+ (0 , 1 ),
146
+ (0 , - 1 ),
147
+ (1 , 0 ),
148
+ (- 1 , 0 ),
149
+ (1 , 1 ),
150
+ (1 , - 1 ),
151
+ (- 1 , 1 ),
152
+ (- 1 , - 1 ),
153
+ ]
131
154
for dir in directions :
132
155
new_i = i + dir [0 ]
133
156
new_j = j + dir [1 ]
134
157
135
158
# If the successor is valid, unblocked, and not visited
136
- if is_valid (new_i , new_j ) and is_unblocked (grid , new_i , new_j ) and not closed_list [new_i ][new_j ]:
159
+ if (
160
+ is_valid (new_i , new_j )
161
+ and is_unblocked (grid , new_i , new_j )
162
+ and not closed_list [new_i ][new_j ]
163
+ ):
137
164
# If the successor is the destination
138
165
if is_destination (new_i , new_j , dest ):
139
166
# Set the parent of the destination cell
@@ -151,7 +178,10 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
151
178
f_new = g_new + h_new
152
179
153
180
# If the cell is not in the open list or the new f value is smaller
154
- if cell_details [new_i ][new_j ].f == float ('inf' ) or cell_details [new_i ][new_j ].f > f_new :
181
+ if (
182
+ cell_details [new_i ][new_j ].f == float ("inf" )
183
+ or cell_details [new_i ][new_j ].f > f_new
184
+ ):
155
185
# Add the cell to the open list
156
186
heapq .heappush (open_list , (f_new , new_i , new_j ))
157
187
# Update the cell details
@@ -165,6 +195,7 @@ def a_star_search(grid: List[List[int]], src: Tuple[int, int], dest: Tuple[int,
165
195
if not found_dest :
166
196
print ("Failed to find the destination cell" )
167
197
198
+
168
199
# Driver Code
169
200
170
201
@@ -191,7 +222,7 @@ def main() -> None:
191
222
[1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 ],
192
223
[1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 ],
193
224
[1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ],
194
- [1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1 ]
225
+ [1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1 ],
195
226
]
196
227
197
228
# Define the source and destination
0 commit comments