de este codigo tienes que asegurarte de que existe como minimo una solucion al laberinto antes de aceptar uno: "import tkinter as tk
import random
import time

class MazeApp:
    def __init__(self, master):
        self.master = master
        self.master.title("Generador y Resolver Laberinto")
        self.canvas = tk.Canvas(master, width=500, height=500)
        self.canvas.pack()

        self.rows = 20
        self.columns = 20
        self.cell_size = 25
        self.grid = None
        self.start = (0, 0)
        self.end = (self.rows - 1, self.columns - 1)
        self.solution_path = []

        self.create_maze()
        self.draw_maze()

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

        self.reset_button = tk.Button(master, text="Generar Nuevo Laberinto", command=self.reset_maze)
        self.reset_button.pack(side='right')

    def create_maze(self):
        self.grid = [[0 for _ in range(self.columns)] for _ in range(self.rows)]
        self._divide(0, 0, self.columns, self.rows)

    def _divide(self, x, y, width, height):
        if width < 2 or height < 2:
            return

        horizontal = random.choice([True, False])
        if width > height:
            horizontal = False
        elif height > width:
            horizontal = True

        if horizontal:
            if height <= 1:
                return
            wall_y = y + random.randint(1, height - 1)
            hole_x = x + random.randint(0, width - 1)

            for i in range(x, x + width):
                if i == hole_x:
                    self.grid[wall_y][i] = 0
                else:
                    self.grid[wall_y][i] = 1

            self._divide(x, y, width, wall_y - y)
            self._divide(x, wall_y + 1, width, y + height - wall_y - 1)
        else:
            if width <= 1:
                return
            wall_x = x + random.randint(1, width - 1)
            hole_y = y + random.randint(0, height - 1)

            for i in range(y, y + height):
                if i == hole_y:
                    self.grid[i][wall_x] = 0
                else:
                    self.grid[i][wall_x] = 1

            self._divide(x, y, wall_x - x, height)
            self._divide(wall_x + 1, y, x + width - wall_x - 1, height)

    def draw_maze(self):
        self.canvas.delete("all")
        for r in range(self.rows):
            for c in range(self.columns):
                color = "black" if self.grid[r][c] == 1 else "white"
                self.canvas.create_rectangle(c * self.cell_size, r * self.cell_size,
                                             (c + 1) * self.cell_size,
                                             (r + 1) * self.cell_size, fill=color, outline='gray')

        # Marca el inicio y el final
        self.canvas.create_rectangle(self.start[1] * self.cell_size, self.start[0] * self.cell_size,
                                     (self.start[1] + 1) * self.cell_size,
                                     (self.start[0] + 1) * self.cell_size, fill="green")  # Inicio

        self.canvas.create_rectangle(self.end[1] * self.cell_size, self.end[0] * self.cell_size,
                                     (self.end[1] + 1) * self.cell_size,
                                     (self.end[0] + 1) * self.cell_size, fill="red")  # Fin

    def solve_maze(self):
        self.solution_path = []
        visited = set()
        if self._solve_dfs(self.start[0], self.start[1], visited):
            self.animate_solution()
        else:
            print("No hay solución para el laberinto.")

    def _solve_dfs(self, r, c, visited):
        if (r, c) == self.end:
            self.solution_path.append((r, c))
            return True

        if (r < 0 or r >= self.rows or
            c < 0 or c >= self.columns or
            self.grid[r][c] == 1 or
            (r, c) in visited):
            return False

        visited.add((r, c))
        self.solution_path.append((r, c))

        # Explorar en cada dirección
        if (self._solve_dfs(r + 1, c, visited) or
            self._solve_dfs(r - 1, c, visited) or
            self._solve_dfs(r, c + 1, visited) or
            self._solve_dfs(r, c - 1, visited)):
            return True

        self.solution_path.pop()
        return False

    def animate_solution(self):
        for r, c in self.solution_path:
            if (r, c) != self.start and (r, c) != self.end:
                self.canvas.create_rectangle(c * self.cell_size, r * self.cell_size,
                                             (c + 1) * self.cell_size,
                                             (r + 1) * self.cell_size, fill="blue")
                self.master.update()
                time.sleep(0.02)

    def reset_maze(self):
        self.solution_path = []
        self.create_maze()
        self.draw_maze()

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