7
7
8
8
class Cell :
9
9
def __init__ (self ):
10
- # Parent cell's row index
10
+ # Parent cell's row index
11
11
self .parent_i = 0
12
- # Parent cell's column index
12
+ # Parent cell's column index
13
13
self .parent_j = 0
14
- # Total cost of the cell (g + h)
15
- self .f = float (' inf' )
16
- # Cost from start to this cell
17
- self .g = float (' inf' )
18
- # Heuristic cost from this cell to destination
14
+ # Total cost of the cell (g + h)
15
+ self .f = float (" inf" )
16
+ # Cost from start to this cell
17
+ self .g = float (" inf" )
18
+ # Heuristic cost from this cell to destination
19
19
self .h = 0
20
20
21
21
@@ -29,24 +29,28 @@ def __init__(self):
29
29
def is_valid (row , col ):
30
30
return (row >= 0 ) and (row < ROW ) and (col >= 0 ) and (col < COL )
31
31
32
+
32
33
# Check if a cell is unblocked
33
34
34
35
35
36
def is_unblocked (grid , row , col ):
36
37
return grid [row ][col ] == 1
37
38
39
+
38
40
# Check if a cell is the destination
39
41
40
42
41
43
def is_destination (row , col , dest ):
42
44
return row == dest [0 ] and col == dest [1 ]
43
45
46
+
44
47
# Calculate the heuristic value of a cell (Euclidean distance to destination)
45
48
46
49
47
50
def calculate_h_value (row , col , dest ):
48
51
return ((row - dest [0 ]) ** 2 + (col - dest [1 ]) ** 2 ) ** 0.5
49
52
53
+
50
54
# Trace the path from source to destination
51
55
52
56
@@ -57,7 +61,10 @@ def trace_path(cell_details, dest):
57
61
col = dest [1 ]
58
62
59
63
# Trace the path from destination to source using parent cells
60
- while not (cell_details [row ][col ].parent_i == row and cell_details [row ][col ].parent_j == col ):
64
+ while not (
65
+ cell_details [row ][col ].parent_i == row
66
+ and cell_details [row ][col ].parent_j == col
67
+ ):
61
68
path .append ((row , col ))
62
69
temp_row = cell_details [row ][col ].parent_i
63
70
temp_col = cell_details [row ][col ].parent_j
@@ -74,6 +81,7 @@ def trace_path(cell_details, dest):
74
81
print ("->" , i , end = " " )
75
82
print ()
76
83
84
+
77
85
# Implement the A* search algorithm
78
86
79
87
@@ -84,7 +92,9 @@ def a_star_search(grid, src, dest):
84
92
return
85
93
86
94
# Check if the source and destination are unblocked
87
- if not is_unblocked (grid , src [0 ], src [1 ]) or not is_unblocked (grid , dest [0 ], dest [1 ]):
95
+ if not is_unblocked (grid , src [0 ], src [1 ]) or not is_unblocked (
96
+ grid , dest [0 ], dest [1 ]
97
+ ):
88
98
print ("Source or the destination is blocked" )
89
99
return
90
100
@@ -125,14 +135,26 @@ def a_star_search(grid, src, dest):
125
135
closed_list [i ][j ] = True
126
136
127
137
# For each direction, check the successors
128
- directions = [(0 , 1 ), (0 , - 1 ), (1 , 0 ), (- 1 , 0 ),
129
- (1 , 1 ), (1 , - 1 ), (- 1 , 1 ), (- 1 , - 1 )]
138
+ directions = [
139
+ (0 , 1 ),
140
+ (0 , - 1 ),
141
+ (1 , 0 ),
142
+ (- 1 , 0 ),
143
+ (1 , 1 ),
144
+ (1 , - 1 ),
145
+ (- 1 , 1 ),
146
+ (- 1 , - 1 ),
147
+ ]
130
148
for dir in directions :
131
149
new_i = i + dir [0 ]
132
150
new_j = j + dir [1 ]
133
151
134
152
# If the successor is valid, unblocked, and not visited
135
- if is_valid (new_i , new_j ) and is_unblocked (grid , new_i , new_j ) and not closed_list [new_i ][new_j ]:
153
+ if (
154
+ is_valid (new_i , new_j )
155
+ and is_unblocked (grid , new_i , new_j )
156
+ and not closed_list [new_i ][new_j ]
157
+ ):
136
158
# If the successor is the destination
137
159
if is_destination (new_i , new_j , dest ):
138
160
# Set the parent of the destination cell
@@ -150,7 +172,10 @@ def a_star_search(grid, src, dest):
150
172
f_new = g_new + h_new
151
173
152
174
# If the cell is not in the open list or the new f value is smaller
153
- if cell_details [new_i ][new_j ].f == float ('inf' ) or cell_details [new_i ][new_j ].f > f_new :
175
+ if (
176
+ cell_details [new_i ][new_j ].f == float ("inf" )
177
+ or cell_details [new_i ][new_j ].f > f_new
178
+ ):
154
179
# Add the cell to the open list
155
180
heapq .heappush (open_list , (f_new , new_i , new_j ))
156
181
# Update the cell details
@@ -164,6 +189,7 @@ def a_star_search(grid, src, dest):
164
189
if not found_dest :
165
190
print ("Failed to find the destination cell" )
166
191
192
+
167
193
# Driver Code
168
194
169
195
@@ -178,7 +204,7 @@ def main():
178
204
[1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 ],
179
205
[1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 ],
180
206
[1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ],
181
- [1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1 ]
207
+ [1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1 ],
182
208
]
183
209
184
210
# Define the source and destination
0 commit comments