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
        
    def 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.f

def 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()
    
    maze = []
    for i, line in enumerate(lines):
        row = [Vertex(i, j, int(t)) for j, t in enumerate(line.strip().split())]
        maze.append(row)
    return maze

def 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)

    while Q:
        current = heapq.heappop(Q)
        if current.x == goal.x and current.y == goal.y:
            return True

        U.append(current)
        for neighbor in get_neighbors(maze, current, n, m):
            ts = current.g + cost(current, neighbor)
            if in_list(U, neighbor) and ts >= neighbor.g:
                continue
            if not in_list(Q, neighbor):
                neighbor.g = ts
                neighbor.h = heuristic(neighbor, goal)
                neighbor.f = neighbor.g + neighbor.h
                neighbor.parent = current
                heapq.heappush(Q, neighbor)
            elif ts < neighbor.g:
                neighbor.g = ts
                neighbor.h = heuristic(neighbor, goal)
                neighbor.f = neighbor.g + neighbor.h
                neighbor.parent = current
    return False

def main():
    n, m = 3, 6
    maze = generate_maze(1, n, m)
    maze[0][0].t = 1
    maze[-1][-1].t = 1
    
    print(“Сгенерированный лабиринт:”)
    print_maze(maze)
    
    start, goal = maze[0][0], maze[-1][-1]

    if AStar(start, goal, maze, n, m):
        path = get_path(goal)
        path.reverse()
        print(“Найденный путь:”)
        for step in path:
            print(step)
        save_path_to_file(path)
        print(“Путь сохранен в файл path.txt”)
    else:
        print(“Путь не найден.”)

if name == ‘main’:
    main() программа не видит name и не запускается