# Visualisation de la transformation Sudoku -> Couverture Exacte
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6))
# Sudoku 4x4 pour simplifier (9x9 serait trop grand)
sudoku_grid = np.array([
[1, 0, 0, 2],
[0, 0, 3, 0],
[0, 4, 0, 0],
[3, 0, 0, 1]
])
# Grille Sudoku
for i in range(4):
for j in range(4):
val = sudoku_grid[i, j]
color = '#FFE5E5' if val != 0 else '#F0F0F0'
rect = plt.Rectangle((j, 3-i), 1, 1, facecolor=color, edgecolor='black')
ax1.add_patch(rect)
if val != 0:
ax1.text(j+0.5, 2.5-i, str(val), ha='center', va='center',
fontsize=16, fontweight='bold')
# Grille de blocs 2x2
for i in range(3):
ax1.axhline(i, color='black', linewidth=2)
for j in range(3):
ax1.axvline(j, color='black', linewidth=2)
ax1.set_xlim(0, 4)
ax1.set_ylim(0, 4)
ax1.set_aspect('equal')
ax1.axis('off')
ax1.set_title('Sudoku 4x4', fontsize=14, fontweight='bold')
# Matrice de couverture exacte (simplifiee)
# Pour Sudoku 4x4:
# - 4*4*4 = 64 possibilites (chaque case peut avoir chaque chiffre 1-4)
# - 4 types de contraintes * 4 * 4 = 64 contraintes
# - Chaque placement satisfait 4 contraintes
# Extrait de la matrice (quelques lignes)
matrice_extrait = [
[1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0], # (0,0,1): case (0,0), chiffre 1
[0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0], # (0,0,2)
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0], # (0,1,3)
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0], # (1,2,3)
[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0], # (2,1,4)
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1], # (3,0,3)
]
# Afficher la matrice
for i, row in enumerate(matrice_extrait[:6]):
for j, val in enumerate(row):
if val == 1:
color = '#4CAF50'
else:
color = '#E0E0E0'
rect = plt.Rectangle((j, 5.5-i), 1, 1, facecolor=color, edgecolor='gray', linewidth=0.5)
ax2.add_patch(rect)
ax2.set_xlim(0, 16)
ax2.set_ylim(0, 6)
ax2.set_aspect('equal')
ax2.axis('off')
# Annotations
ax2.text(8, 6.2, 'Matrice de Couverture Exacte', ha='center', fontsize=12, fontweight='bold')
ax2.text(8, -0.5, 'Colonnes: Cellules | Lignes | Colonnes | Blocs', ha='center', fontsize=9)
ax2.text(8, -1, 'Lignes: Placements (ligne, colonne, chiffre)', ha='center', fontsize=9)
# Legendes des colonnes
col_labels = ['C00', 'C01', 'C02', 'C03', 'L0-1', 'L0-2', 'L1-3', 'L1-4',
'Col0-1', 'Col1-3', 'Col1-4', 'Col3-3', 'B0-1', 'B1-3', 'B2-4', 'B3-3']
for j, label in enumerate(col_labels):
ax2.text(j+0.5, 5.7, label, ha='center', va='bottom', fontsize=6, rotation=45)
row_labels = ['(0,0,1)', '(0,0,2)', '(0,1,3)', '(1,2,3)', '(2,1,4)', '(3,0,3)']
for i, label in enumerate(row_labels):
ax2.text(-0.5, 5-i, label, ha='right', va='center', fontsize=7)
plt.suptitle('Transformation Sudoku -> Probleme de Couverture Exacte',
fontsize=14, fontweight='bold')
plt.tight_layout()
plt.show()
print("\nTransformation Sudoku en Couverture Exacte:")
print(" - Chaque placement (ligne, colonne, chiffre) devient une ligne de la matrice")
print(" - Chaque contrainte devient une colonne de la matrice")
print(" - Un 1 indique que le placement satisfait la contrainte")
print(" - Solution: selectionner des lignes pour couvrir toutes les colonnes exactement une fois")