dibuja mejor las paredes ya que ahora al resolverlo parecec como si atravesara las paredes: "import tkinter as tk
import random
import time

class Cell:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.walls = [True, True, True, True]  # Top, Right, Bottom, Left
        self.visited = False

    def get_neighbors(self, grid):
        neighbors = []
        directions = [(0, -1), (1, 0), (0, 1), (-1, 0)]  # (dx, dy)
        for i, (dx, dy) in enumerate(directions):
            nx, ny = self.x + dx, self.y + dy
            if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and not grid[nx][ny].visited:
                neighbors.append((grid[nx][ny], i))
        return neighbors

    def get_accessible_neighbors(self, grid):
        """
        Retorna las celdas vecinas a las que se puede acceder (sin muro entre ellas).
        """
        accessible = []
        directions = [(0, -1), (1, 0), (0, 1), (-1, 0)]  # Top, Right, Bottom, Left
        for i, (dx, dy) in enumerate(directions):
            if not self.walls[i]:  # Si no hay muro en esta dirección
                nx, ny = self.x + dx, self.y + dy
                if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]):
                    accessible.append(grid[nx][ny])
        return accessible


class Maze:
    def __init__(self, cols, rows):
        self.cols = cols
        self.rows = rows
        self.grid = [[Cell(x, y) for y in range(rows)] for x in range(cols)]
        self.stack = []
        self.start = self.grid[0][0]
        self.end = self.grid[cols-1][rows-1]

    def generate(self):
        current = self.start
        current.visited = True

        while True:
            neighbors = current.get_neighbors(self.grid)
            if neighbors:
                neighbor, direction = random.choice(neighbors)
                current.walls[direction] = False
                neighbor.walls[(direction + 2) % 4] = False
                self.stack.append(current)
                current = neighbor
                current.visited = True
            elif self.stack:
                current = self.stack.pop()
            else:
                break

    def solve(self):
        stack = [self.start]
        visited = set()

        while stack:
            current = stack[-1]

            if current == self.end:
                return stack  # Retorna la ruta encontrada

            visited.add(current)
            neighbors = current.get_accessible_neighbors(self.grid)
            unvisited_neighbors = [n for n in neighbors if n not in visited]

            if unvisited_neighbors:
                next_cell = unvisited_neighbors[0]  # Puedes elegir el primero o aleatoriamente
                stack.append(next_cell)
            else:
                stack.pop()

        return []  # Retorna una lista vacía si no se encuentra solución (teóricamente no debería pasar)


class MazeApp:
    def __init__(self, master):
        self.master = master
        master.title("Laberinto")

        self.cells = 20
        self.canvas = tk.Canvas(master, width=600, height=600, bg="white")
        self.canvas.pack()

        self.generate_button = tk.Button(master, text="Generar Laberinto", command=self.generate_maze)
        self.generate_button.pack()

        self.solve_button = tk.Button(master, text="Resolver Laberinto", command=self.solve_maze)
        self.solve_button.pack()

        self.maze = None
        self.start_color = "green"
        self.end_color = "red"

    def draw_maze(self):
        self.canvas.delete("all")
        cell_size = 600 // self.cells

        for x in range(self.cells):
            for y in range(self.cells):
                cell = self.maze.grid[x][y]
                if cell.walls[0]:
                    self.canvas.create_line(y * cell_size, x * cell_size, (y + 1) * cell_size, x * cell_size)
                if cell.walls[1]:
                    self.canvas.create_line((y + 1) * cell_size, x * cell_size, (y + 1) * cell_size, (x + 1) * cell_size)
                if cell.walls[2]:
                    self.canvas.create_line(y * cell_size, (x + 1) * cell_size, (y + 1) * cell_size, (x + 1) * cell_size)
                if cell.walls[3]:
                    self.canvas.create_line(y * cell_size, x * cell_size, y * cell_size, (x + 1) * cell_size)

        # Marcamos el inicio y el fin
        self.canvas.create_rectangle(0 * cell_size + 1, 0 * cell_size + 1, 
                                      (0 + 1) * cell_size - 1, (0 + 1) * cell_size - 1, 
                                      fill=self.start_color)
        self.canvas.create_rectangle((self.cells - 1) * cell_size + 1, 
                                      (self.cells - 1) * cell_size + 1,
                                      (self.cells) * cell_size - 1, 
                                      (self.cells) * cell_size - 1, 
                                      fill=self.end_color)

    def generate_maze(self):
        self.maze = Maze(self.cells, self.cells)
        self.maze.generate()
        self.draw_maze()  # Dibujar el laberinto generado

    def solve_maze(self):
        if not self.maze:
            return  # Asegura que haya un laberinto generado

        path = self.maze.solve()
        cell_size = 600 // self.cells
        self.canvas.delete("path")  # Limpiar rutas anteriores

        if not path:
            print("No se encontró una solución al laberinto.")
            return

        # Visualización de la ruta
        for cell in path:
            self.canvas.create_rectangle(cell.y * cell_size + 2, cell.x * cell_size + 2,
                                         (cell.y + 1) * cell_size - 2, 
                                         (cell.x + 1) * cell_size - 2, 
                                         fill='blue', tags="path")
            self.master.update()
            time.sleep(0.05)  # Pausa para visualizar el proceso de resolución

        # Marcar el final de la ruta
        end_cell = path[-1]
        self.canvas.create_rectangle(end_cell.y * cell_size + 2, end_cell.x * cell_size + 2,
                                     (end_cell.y + 1) * cell_size - 2, 
                                     (end_cell.x + 1) * cell_size - 2, 
                                     fill='yellow', tags="path")


if __name__ == "__main__":
    root = tk.Tk()
    app = MazeApp(root)
    root.mainloop()"