def scan_grid(grid, start=(0, 0)): """Scan all grid, so we can find a path from 'start' to any point""" q = deque() q.append(start) came_from = {start: None} while len(q) > 0: current_pos = q.popleft() neighbors = gh.get_neighbors(grid, current_pos[0], current_pos[1]) for neighbor in neighbors: if neighbor not in came_from: q.append(neighbor) came_from[neighbor] = current_pos return came_from
def find_path_greedy(grid, start, end): pq = PriorityQueue() pq.put((0, start)) came_from = {start: None} while not pq.empty(): current_pos = pq.get()[1] if current_pos == end: break neighbors = gh.get_neighbors(grid, current_pos[0], current_pos[1]) for neighbor in neighbors: if neighbor not in came_from: priority = gh.heuristic_distance(neighbor, end, type="m") pq.put((priority, neighbor)) came_from[neighbor] = current_pos return came_from
def find_path_a_star(grid, start, end): pq = PriorityQueue() pq.put((0, start)) came_from = {start: None} costs = {start: 0} while not pq.empty(): current_pos = pq.get()[1] if current_pos == end: break neighbors = gh.get_neighbors(grid, current_pos[0], current_pos[1]) for neighbor in neighbors: new_cost = costs[current_pos] + gh.get_cost(grid, neighbor) if neighbor not in costs or new_cost < costs[neighbor]: costs[neighbor] = new_cost priority = new_cost + gh.heuristic_distance(neighbor, end) pq.put((priority, neighbor)) came_from[neighbor] = current_pos return came_from