import random import heapq class Vertex: def init(self, x, y, t): self.x = x self.y = y…
- User
import random import heapq
class Vertex: def init(self, x, y, t): self.x = x self.y = y self.t = t self.g = float(‘inf’) self.h = float(‘inf’) self.f = float(‘inf’) self.parent = None
codedef copy(self): v = Vertex(self.x, self.y, self.t) v.g = self.g v.h = self.h v.f = self.f v.parent = self.parent return v def lt(self, other): return self.f < other.fdef heuristic(v, goal): return max(abs(v.x - goal.x), abs(v.y - goal.y))
def cost(current, neighbor): if current.x != neighbor.x and current.y != neighbor.y: # Diagonal move return neighbor.t * 1.4 return neighbor.t
def in_list(lst, vertex): for v in lst: if v.x == vertex.x and v.y == vertex.y: return True return False
def get_neighbors(maze, current, n, m): neighbors = [] for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)]: x, y = current.x + dx, current.y + dy if 0 <= x < n and 0 <= y < m and maze[x][y].t != 0: neighbors.append(maze[x][y]) return neighbors
def print_maze(maze): for row in maze: print(" “.join(str(vertex.t) for vertex in row))
def get_path(goal, path=[]): if goal.parent: path = get_path(goal.parent, path) path.append(f”({goal.x}, {goal.y}, {goal.g})“) return path
def generate_maze(mode=1, n=3, m=6): if mode == 1: if n <= 0 or m <= 0: raise Exception(“Размеры лабиринта должны быть положительными числами.”) maze = [[Vertex(i, j, random.randint(1, 4)) for j in range(m)] for i in range(n)] return maze else: return get_from_file()
def get_from_file(): with open(“somefile.txt”, “r”) as file: lines = file.readlines()
codemaze = [] for i, line in enumerate(lines): row = [Vertex(i, j, int(t)) for j, t in enumerate(line.strip().split())] maze.append(row) return mazedef save_path_to_file(path): with open(“path.txt”, “w”) as file: for item in path: file.write(f”{item}\n")
def AStar(start, goal, maze, n, m): Q = [] U = [] start.g = 0 start.f = start.g + heuristic(start, goal) heapq.heappush(Q, start)
code - Assistant
- User
Выводит ошибку: не удалось запустить адаптер отладки
- Assistant
- User
name is not defended
- Assistant
- User
При запуске в консоли выдаёт ошибку Attribute Error: 'tuple object has no attribute 'x'
- Assistant
- User
Type Error: '<' not supported between instance s of 'Vertex' and 'Vertex'
- Assistant