haz que al menos tenga una solucion el laberinto: "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


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]
        path = []
        visited = set()

        while stack:
            current = stack[-1]
            if current == self.end:
                break
            
            visited.add(current)
            neighbors = current.get_neighbors(self.grid)

            unvisited_neighbors = [n[0] for n in neighbors if n[0] not in visited]
            if unvisited_neighbors:
                next_cell = random.choice(unvisited_neighbors)
                stack.append(next_cell)
            else:
                stack.pop()

        return stack


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):
        path = self.maze.solve()
        cell_size = 600 // self.cells
        self.canvas.delete("path")  # Limpiar rutas anteriores

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

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