自分ならこう書く - pythonでA* - ラシウラより def astar(init, goal, nexts, distance=lambda path: len(path), heuristic=lambda pos: 0): import heapq queue = [] checked = [init] heapq.heappush(queue, (distance([init]) + heuristic(init), [init])) while len(queue) > 0: score, path = heapq.heappop(queue) last = path[-1] if last == goal: return path for pos in nexts(last): if pos in checked: continue checked.append(pos)