public byte[,] GeneratePuzzleWithClues(int n) { if (n < 17) { Console.WriteLine("Not possible!"); } var exactCover = new ExactCover(); byte[,] completedSudoku; byte[,] cluesRemoved; int attempts = 0; do { completedSudoku = GenerateRandomCompleteGrid(); cluesRemoved = RemoveClues(completedSudoku, 81 - n); attempts++; }while (!exactCover.CheckExactlyOneSolution(GenerateExactCoverProblem(cluesRemoved))); Console.WriteLine("Attempts: " + attempts); Print(completedSudoku); Console.WriteLine("#########"); Print(cluesRemoved); return(cluesRemoved); }
public override void OnUncover(ExactCover ec) { if (cageOptional != null) { cageOptional.OnUncover(ec, this); } }
public void OnCover(ExactCover ec, SudokuCandidate sc) { int cage = sc.c; int num = sc.n + 1; System.Diagnostics.Debug.Assert(info.sizes[cage] > 0); SudokuSolver ss = (SudokuSolver)ec; info.remaining_totals[cage] -= num; info.remaining_sizes[cage]--; removed = CheckRemains(ss, info, cage, num); }
public byte[,] GenerateRandomCompleteGrid() { byte[,] exactCoverProblem = GenerateExactCoverProblem(new byte[9, 9] { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, }); List <int> rowsRandomOrder; (exactCoverProblem, rowsRandomOrder) = RandomiseMatrix(exactCoverProblem); var exactCover = new ExactCover(); var result = exactCover.GetFirstSolution(exactCoverProblem); return(ExactCoverResultToSudoku(result, rowsRandomOrder)); }
public void OnUncover(ExactCover ec, SudokuCandidate sc) { if (removed != null) { for (int i = removed.Length - 1; i >= 0; --i) { ec.UndiscardCandidate(removed[i]); } } int num = sc.n + 1; info.remaining_totals[sc.c] += num; info.remaining_sizes[sc.c]++; }
public byte[,] Solve(byte[,] input) { byte[,] exactCoverProblem = GenerateExactCoverProblem(input); var exactCover = new ExactCover(); var results = exactCover.GetAllSolutions(exactCoverProblem); if (results.Count > 1) { throw new ArgumentException("More than one solution exists."); } else { return(ExactCoverResultToSudoku(results[0])); } }