code a python script that generates random complete non overlapping fills of a 2n by 2n…
- User
code a python script that generates random complete non overlapping fills of a 2n by 2n square with 1 by 2 and 2 by 1 dominos and renders it
Assistant
Here's a Python script that generates random complete non-overlapping fills of a 2n×2n square with 1×2 and 2×1 dominos and renders the result:
import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patches import random def generate_domino_tiling(n): """Generate a random domino tiling of a 2n×2n grid.""" size = 2 * n grid = np.zeros((size, size), dtype=int) # 0 means empty # Keep track of available cells available = [(i, j) for i in range(size) for j in range(size)] dominos = [] # Continue until no more cells are available or no valid moves while available: # Pick a random available cell if not available: break i, j = random.choice(available) if grid[i, j] != 0: # If already filled, remove from available available.remove((i, j)) continue # Try to place a domino in one of the four possible directions directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] random.shuffle(directions) placed = False for di, dj in directions: ni, nj = i + di, j + dj if 0 <= ni < size and 0 <= nj < size and grid[ni, nj] == 0: # Place a domino domino_id = len(dominos) + 1 grid[i, j] = domino_id grid[ni, nj] = domino_id dominos.append(((i, j), (ni, nj))) # Remove these cells from available available.remove((i, j)) if (ni, nj) in available: available.remove((ni, nj)) placed = True break if not placed: # If we couldn't place a domino, remove this cell from available available.remove((i, j)) # Check if the tiling is complete if np.any(grid == 0): # If not complete, try again return generate_domino_tiling(n) return grid, dominos def render_tiling(grid, dominos, n): """Render the domino tiling.""" size = 2 * n fig, ax = plt.subplots(figsize=(8, 8)) # Set up the plot ax.set_xlim(0, size) ax.set_ylim(0, size) ax.set_aspect('equal') ax.set_xticks(range(size + 1)) ax.set_yticks(range(size + 1)) ax.grid(True) ax.set_title(f"Random Domino Tiling of {size}×{size} Grid") # Generate colors for dominos colors = plt.cm.tab20(np.linspace(0, 1, len(dominos))) random.shuffle(colors) # Draw each domino for idx, ((i1, j1), (i2, j2)) in enumerate(dominos): # Determine the rectangle coordinates min_i, max_i = min(i1, i2), max(i1, i2) min_j, max_j = min(j1, j2), max(j1, j2) width = max_j - min_j + 1 height = max_i - min_i + 1 # Create rectangle rect = patches.Rectangle( (min_j, min_i), width, height, linewidth=2, edgecolor='black', facecolor=colors[idx % len(colors)] ) ax.add_patch(rect) plt.show() def generate_and_render_tiling(n): """Generate and render a random domino tiling of a 2n×2n grid.""" print(f"Generating a random domino tiling for a {2*n}×{2*n} grid...") grid, dominos = generate_domino_tiling(n) render_tiling(grid, dominos, n) print(f"Tiling complete with {len(dominos)} dominos.") # Example usage if __name__ == "__main__": n = int(input("Enter n for a 2n×2n grid: ")) generate_and_render_tiling(n)Would you like me to explain or break down the code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.