public Sudoku ResoudreSudoku(Sudoku s) { var internalRows = BuildInternalRowsForGrid(s); var dlxRows = BuildDlxRows(internalRows); var solutions = new Dlx() .Solve(dlxRows, d => d, r => r) .Where(solution => VerifySolution(internalRows, solution)) .ToImmutableList(); Console.WriteLine(); if (solutions.Any()) { Console.WriteLine($"First solution (of {solutions.Count}):"); Console.WriteLine(); DrawSolution(internalRows, solutions.First()); Console.WriteLine(); } else { Console.WriteLine("No solutions found!"); } Console.Read(); return(s); }
//Création d'une méthode qui prend en entrée un string (le sudoku non résolu) et qui renvoie un string (le sudoku résolu). //De base le code duquel on s'est inspiré ne renvoyait rien, la sortie était sous forme d'un Console.WriteLine() private static string Sudokusolution(string sudoku) { //Récupération du sudoku à résoudre depuis le string en entrée et transfert dans une ImmutableList. var grid = new Grid(ImmutableList.Create( sudoku.Substring(0, 9), sudoku.Substring(9, 9), sudoku.Substring(18, 9), sudoku.Substring(27, 9), sudoku.Substring(36, 9), sudoku.Substring(45, 9), sudoku.Substring(54, 9), sudoku.Substring(63, 9), sudoku.Substring(72, 9))); var internalRows = BuildInternalRowsForGrid(grid); var dlxRows = BuildDlxRows(internalRows); var solutions = new Dlx() .Solve(BuildDlxRows(internalRows), d => d, r => r) .Where(solution => VerifySolution(internalRows, solution)) .ToImmutableList(); if (solutions.Any()) { //Enlever commentaire pour afficher les solutions dans la console //Console.WriteLine($"First solution (of {solutions.Count}):"); //Console.WriteLine(); //SolutionToGrid(internalRows, solutions.First()).Draw(); //Console.WriteLine(); //Ajout de ce bout de code pour avoir une sortie de type string contenant le sudoku résolu. string s = ""; for (int i = 0; i <= 8; i++) { for (int j = 0; j <= 8; j++) { s += SolutionToGrid(internalRows, solutions.First()).ValueAt(i, j).ToString(); } } return(s); } else { //Console.WriteLine("No solutions found!"); return("No solutions found!"); } }
public void ExactCoverProblemsWithNoSolutionsTest() { Func <int, string> makeLabel = numSolutions => string.Format( "Expected no solutions but got {0}", numSolutions); var arbMatrix = Arb.From(GenMatrixOfIntWithNoSolutions()); var property = Prop.ForAll(arbMatrix, matrix => { var solutions = new Dlx().Solve(matrix).ToList(); return((!solutions.Any()).Label(makeLabel(solutions.Count()))); }); Check.One(Config, property); }
public Sudoku Solve(Sudoku s) { var currentGrid = SudokuVersGrid(s); var internalRows = BuildInternalRowsForGrid(currentGrid); var dlxRows = BuildDlxRows(internalRows); ImmutableList <Solution> solutions = new Dlx() .Solve(dlxRows, d => d, r => r) .Where(solution => VerifySolution(internalRows, solution)) .ToImmutableList(); if (!solutions.Any()) { return(s); } var gridSolution = SolutionToGrid(internalRows, solutions[0]); GridVersSudoku(gridSolution, ref s); return(s); }
public void Solve(GrilleSudoku grid) { var internalRows = BuildInternalRowsForGrid(grid); var dlxRows = BuildDlxRows(internalRows); var solutions = new Dlx() .Solve(dlxRows, d => d, r => r) .Where(solution => VerifySolution(internalRows, solution)) .ToImmutableList(); Console.WriteLine(); if (solutions.Any()) { Console.WriteLine($"First solution (of {solutions.Count}):"); Console.WriteLine(); SolutionToGrid(grid, internalRows, solutions.First()); Console.WriteLine(); } else { Console.WriteLine("No solutions found!"); } }
private static void Main() { // http://puzzles.telegraph.co.uk/site/search_puzzle_number?id=27744 var grid = new Grid(ImmutableList.Create( "6 4 9 7 3", " 3 6 ", " 18", " 18 9", " 43 ", "7 39 ", " 7 ", " 4 8 ", "9 8 6 4 5")); grid.Draw(); var internalRows = BuildInternalRowsForGrid(grid); var dlxRows = BuildDlxRows(internalRows); var solutions = new Dlx() .Solve(dlxRows, d => d, r => r) .Where(solution => VerifySolution(internalRows, solution)) .ToImmutableList(); Console.WriteLine(); if (solutions.Any()) { Console.WriteLine($"First solution (of {solutions.Count}):"); Console.WriteLine(); DrawSolution(internalRows, solutions.First()); Console.WriteLine(); } else { Console.WriteLine("No solutions found!"); } }