using System.Text;
// Exemple canonique : Univers U = {1,2,3,4,5,6,7}
// S1={1,4,7}, S2={1,4}, S3={4,5,7}, S4={3,5,6}, S5={2,3,6,7}, S6={2,7}
// Solution attendue : {S2, S4, S6} car S2∪S4∪S6 = {1,4}∪{3,5,6}∪{2,7} = U
int[,] matrix = {
{ 1, 0, 0, 1, 0, 0, 1 }, // S1
{ 1, 0, 0, 1, 0, 0, 0 }, // S2
{ 0, 0, 0, 1, 1, 0, 1 }, // S3
{ 0, 0, 1, 0, 1, 1, 0 }, // S4
{ 0, 1, 1, 0, 0, 1, 1 }, // S5
{ 0, 1, 0, 0, 0, 0, 1 }, // S6
};
string[] rowNames = { "S1", "S2", "S3", "S4", "S5", "S6" };
int nRows = matrix.GetLength(0), nCols = matrix.GetLength(1);
var sb = new StringBuilder();
sb.AppendLine("=== Couverture exacte : exemple canonique ===");
sb.AppendLine($"Univers U = {{1,2,3,4,5,6,7}} (n={nCols} éléments, m={nRows} sous-ensembles)");
sb.AppendLine();
sb.AppendLine("Matrice binaire :");
sb.AppendLine(" 1 2 3 4 5 6 7");
for (int i = 0; i < nRows; i++)
{
var row = new StringBuilder();
for (int j = 0; j < nCols; j++) row.Append(matrix[i, j] == 1 ? " 1" : " .");
sb.AppendLine($" {rowNames[i]} {row}");
}
sb.AppendLine();
// Vérification de la solution {S2, S4, S6} = indices 1, 3, 5
int[] solRows = { 1, 3, 5 };
int[] coverCount = new int[nCols];
foreach (var r in solRows)
for (int j = 0; j < nCols; j++) coverCount[j] += matrix[r, j];
bool isExact = coverCount.All(c => c == 1);
sb.AppendLine($"Solution proposée : {{S2, S4, S6}}");
sb.AppendLine($"Couverture par colonne : [{string.Join(", ", coverCount)}]");
sb.AppendLine($"Couverture exacte ? {(isExact ? "OUI" : "NON")} (chaque colonne == 1)");
sb.ToString().Display();