Hướng dẫn a-star python - trăn một ngôi sao

#! heuristic = 0]: self.item = itemself . . . self, node]: x, y = node.itemreturn0#-*-mã hóa: utf-8-*-# intert = 0, heuristic = 0]: self.item = itemself.parent = pargeforelf.gscore = gscoreself.heuristic = heuristicdef__eq __ [tự, khác] .heuristicNode.gscore: left = bisect.b isect_left [hàng đợi, trùng lặp] phải = bisect.bisect_right [hàng đợi, trùng lặp] Queue.pop [Queue.index [trùng lặp, trái, phải]] nodeBisect.insort_left [hàng đợi, nút] returnPathClassd2 [astarpathfinding]: def__init __ [self, ma trận, hàng, cols]: self.matrix = matrixelf. .Itemx2, y2 = des.itemreturnabs [x1-x2] +abs [y1-y2] refgetchildren [self, node]: x, y = node.itemreturn [self.node [[x +1, y] , gscore = node.gscore+1], self.node [[x-1, y], Parent = node, gscore = node.gscore+1], self.node [[x, y+1], Parent = node , gscore = node.gscore+1], self.node [[x, y-1], Parent = node, gscore = node.gscore+1],] defavailable [self, node]: x, y = node.itemreturn0 # -*- coding: utf-8 -*- # import bisect class AStarPathfinding[object]: class Node[object]: # item must be hashable def __init__[self, item, parent=None, gscore=0, heuristic=0]: self.item = item self.parent = parent self.gscore = gscore self.heuristic = heuristic def __eq__[self, other]: return other.item == self.item def __lt__[self, other]: return self.gscore + self.heuristic node.gscore: left = bisect.bisect_left[queue, duplicated] right = bisect.bisect_right[queue, duplicated] queue.pop[queue.index[duplicated, left, right]] node.heuristic = self.heuristic[node, target] openpath[node.item] = node bisect.insort_left[queue, node] return path class D2[AStarPathfinding]: def __init__[self, matrix, rows, cols]: self.matrix = matrix self.rows = rows self.cols = cols def heuristic[self, src, des]: x1, y1 = src.item x2, y2 = des.item return abs[x1 - x2] + abs[y1 - y2] def getchildren[self, node]: x, y = node.item return [ self.Node[[x + 1, y], parent=node, gscore=node.gscore + 1], self.Node[[x - 1, y], parent=node, gscore=node.gscore + 1], self.Node[[x, y + 1], parent=node, gscore=node.gscore + 1], self.Node[[x, y - 1], parent=node, gscore=node.gscore + 1], ] def available[self, node]: x, y = node.item return 0

Bài Viết Liên Quan

Chủ Đề