Professional Documents
Culture Documents
A Start Search Assignment
A Start Search Assignment
import heapq
while open_list:
current_cost, current_node = heapq.heappop(open_list)
if current_node == end:
break
closed_set.add(current_node)
if (
0 <= new_node[0] < len(grid)
and 0 <= new_node[1] < len(grid[0])
and grid[new_node[0]][new_node[1]] != '#'
and new_node not in closed_set
):
new_cost = cost_so_far[current_node] + 1
if new_node not in cost_so_far or new_cost < cost_so_far[new_node]:
cost_so_far[new_node] = new_cost
priority = new_cost + heuristic(new_node, end)
heapq.heappush(open_list, (priority, new_node))
came_from[new_node] = current_node
# Reconstruct the path from the end point to the start point
path = []
current = end
while current != start:
path.append(current)
current = came_from[current]
path.append(start)
path.reverse()
Result:
Path Length : 9
Searched squares: 10