/* * latin_squares.c * * Created on: 12/04/2017 * Author: pedro * * https://www.cril.univ-artois.fr/~lecoutre/benchmarks.html# * * Pandiagonal Latin Square problem: * All rows and columns of a matrix must contain different values (taken from 0 to n-1). * */ #include "latin_squares.h" #include #include #include #include "../config.h" #include "../constraints/fake_all_different.h" #include "../split.h" #include "../variables.h" /* * Solve an instance of Latin Square problem with N values */ void run_latin_squares(int* csp_dims) { int n = csp_dims[0]; unsigned long result; int i, j; // vector with the IDs of all the variables unsigned int** vs_id = malloc((unsigned int)n * 2 * sizeof(unsigned int*)); for (i = 0; i < n * 2; i++) { vs_id[i] = malloc((unsigned int)n * 2 * sizeof(unsigned int)); } unsigned int* c_vs_id = malloc((unsigned int)n * sizeof(unsigned int)); // Create all the variables for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { vs_id[i][j] = v_new_range(0, (unsigned int)n - 1, true); } } // horizontal lines all_diff constraints for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { c_vs_id[j] = vs_id[i][j]; } c_fake_all_different(c_vs_id, (unsigned int)n); } // vertical lines all_diff constraints for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { c_vs_id[j] = vs_id[j][i]; } c_fake_all_different(c_vs_id, (unsigned int)n); } if (FINDING_ONE_SOLUTION) { printf("\nFinding one solution for Latin Squares with %u numbers.\n", n); } else { printf("\nCounting all the solutions for Latin Squares with %u numbers.\n", n); } // Solve the CSP result = solve_CSP(); if (FINDING_ONE_SOLUTION && result == 1) { printf("Solution:\n"); for (i = 0; i < n; i++) { vs_print_single_val(vs_id[i], (unsigned int)n, 1); printf("\n"); } } else { printf("%lu solution(s) found\n", result); } for (i = 0; i < n * 2; i++) { free(vs_id[i]); } free(vs_id); free(c_vs_id); }