en este codigo me gustaria que se fuera mostrando en fondo de color azul por donde puede circular el agua ya que las conexiones estan abiertas, asi si se resuelve tiene que estar azul , al menos, del inicio hasta el final: ""import tkinter as tk
from tkinter import messagebox
import random

# Definición de las piezas
PIECES = {
    'line': {
        'chars': ['─', '│', '─', '│'],
        'connections': [
            [0, 1, 0, 1],  # 0°, horizontal
            [1, 0, 1, 0],  # 90°, vertical
            [0, 1, 0, 1],  # 180°, horizontal
            [1, 0, 1, 0],  # 270°, vertical
        ]
    },
    'curve': {
        'chars': ['└', '┌', '┐', '┘'],
        'connections': [
            [1, 1, 0, 0],  # 0°, conecta Arriba y Derecha
            [0, 1, 1, 0],  # 90°, conecta Derecha y Abajo
            [0, 0, 1, 1],  # 180°, conecta Abajo y Izquierda
            [1, 0, 0, 1],  # 270°, conecta Izquierda y Arriba
        ]
    },
    't': {
        'chars': ['┬', '┤', '┴', '├'],
        'connections': [
            [1, 1, 0, 1],  # 0°, conecta Arriba, Derecha, Izquierda
            [1, 1, 1, 0],  # 90°, conecta Arriba, Derecha, Abajo
            [0, 1, 1, 1],  # 180°, conecta Derecha, Abajo, Izquierda
            [1, 0, 1, 1],  # 270°, conecta Arriba, Abajo, Izquierda
        ]
    },
    'cross': {
        'chars': ['┼', '┼', '┼', '┼'],
        'connections': [
            [1, 1, 1, 1],
            [1, 1, 1, 1],
            [1, 1, 1, 1],
            [1, 1, 1, 1],
        ]
    }
}

class Piece:
    def __init__(self, piece_type, rotation=0):
        self.piece_type = piece_type
        self.rotation = rotation

    def connections(self):
        return PIECES[self.piece_type]['connections'][self.rotation]

    def char(self):
        return PIECES[self.piece_type]['chars'][self.rotation]

class Grid:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.grid = [[None for _ in range(width)] for _ in range(height)]

    def set_piece(self, x, y, piece):
        self.grid[y][x] = piece

    def is_valid_connection(self, x1, y1, x2, y2, direction):
        piece1 = self.grid[y1][x1]
        piece2 = self.grid[y2][x2]
        if not piece1 or not piece2:
            return False

        connections1 = piece1.connections()
        connections2 = piece2.connections()

        dir_map = {'up': 0, 'right': 1, 'down': 2, 'left': 3}
        opposite_dir_map = {'up': 2, 'right': 3, 'down': 0, 'left': 1}

        dir_index = dir_map[direction]
        opp_dir_index = opposite_dir_map[direction]

        return connections1[dir_index] == 1 and connections2[opp_dir_index] == 1

    def is_path_valid(self, start_x, start_y, end_x, end_y):
        visited = set()

        def dfs(x, y):
            if (x, y) == (end_x, end_y):
                return True
            visited.add((x, y))

            directions = ['up', 'right', 'down', 'left']
            dxs_dys = [(-1, 0), (0, 1), (1, 0), (0, -1)]

            for dir_idx, (dx, dy) in enumerate(dxs_dys):
                nx, ny = x + dx, y + dy
                if 0 <= nx < self.width and 0 <= ny < self.height:
                    if (nx, ny) not in visited and self.is_valid_connection(x, y, nx, ny, directions[dir_idx]):
                        if dfs(nx, ny):
                            return True

            return False

        return dfs(start_x, start_y)

    def generate_random_board(self):
        for y in range(self.height):
            for x in range(self.width):
                piece_type = random.choice(list(PIECES.keys()))
                rotation = random.randint(0, 3)
                self.set_piece(x, y, Piece(piece_type, rotation))

class App:
    def __init__(self, root):
        self.root = root
        self.root.title("Grid Puzzle Solver")
        
        self.grid = Grid(5, 5)
        self.grid.generate_random_board()  # Generar el tablero aleatorio al inicio.
        self.buttons = [[None for _ in range(5)] for _ in range(5)]
        self.setup_ui()
        self.update_ui()

    def setup_ui(self):
        control_frame = tk.Frame(self.root)
        control_frame.pack(side=tk.TOP, pady=10)
        
        rotation_button = tk.Button(control_frame, text="Rotate Piece", command=self.rotate_piece)
        rotation_button.pack(side=tk.LEFT, padx=5)

        verify_button = tk.Button(control_frame, text="Verify", command=self.verify_solution)
        verify_button.pack(side=tk.LEFT, padx=5)

        solve_button = tk.Button(control_frame, text="Solve", command=self.solve_puzzle)
        solve_button.pack(side=tk.LEFT, padx=5)

        grid_frame = tk.Frame(self.root)
        grid_frame.pack(pady=10)

        for y in range(5):
            for x in range(5):
                button = tk.Button(grid_frame, width=5, height=2,
                                   font=("Courier", 20),
                                   command=lambda row=y, col=x: self.place_piece(row, col))
                button.grid(row=y, column=x)
                self.buttons[y][x] = button

        # Establecer los colores de fondo para el inicio y el final
        self.buttons[0][0].config(bg="lightgreen")
        self.buttons[4][4].config(bg="lightcoral")

    def rotate_piece(self):
        self.current_rotation = (self.current_rotation + 1) % 4

    def place_piece(self, row, col):
        piece = self.grid.grid[row][col]
        piece.rotation = (piece.rotation + 1) % 4
        self.update_ui()

    def update_ui(self):
        for y in range(5):
            for x in range(5):
                piece = self.grid.grid[y][x]
                if piece:
                    self.buttons[y][x].config(text=piece.char())
                else:
                    self.buttons[y][x].config(text="")

    def verify_solution(self):
        if self.grid.is_path_valid(0, 0, 4, 4):
            messagebox.showinfo("Result", "Valid path found!")
        else:
            messagebox.showerror("Result", "No valid path found.")

    def solve_puzzle(self):
        # Esto es un método sencillo que intenta encontrar una solución válida, ajusta las rotaciones.
        for y in range(self.grid.height):
            for x in range(self.grid.width):
                piece = self.grid.grid[y][x]
                if piece:
                    for rotation in range(4):
                        piece.rotation = rotation
                        if self.grid.is_path_valid(0, 0, 4, 4):
                            self.update_ui()
                            messagebox.showinfo("Result", "Solved!")
                            return
        messagebox.showerror("Result", "No solution found.")
        self.update_ui()

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