static List <Coordinate_Sudoku> SolveSolution(List <Coordinate_Sudoku> Origin, List <Coordinate_Sudoku> Solution, int row, int col, int SolutionCount) { if (!Origin.Exists(c => c.Row == row && c.Column == col)) { for (int value = 1; value < 10; value++) { Coordinate_Sudoku temp = new Coordinate_Sudoku(row, col, value, returnBlock(row, col)); if (isSafe(Origin, Solution, temp)) { Solution.Add(temp); if (Solution.Count < SolutionCount) { if (col < 9) { SolveSolution(Origin, Solution, row, col + 1, SolutionCount); } else { SolveSolution(Origin, Solution, row + 1, 1, SolutionCount); } } if (Solution.Count < SolutionCount && Solution.Count > 0) { Solution.RemoveAt(Solution.Count - 1); } } } } else { if (Solution.Count < SolutionCount) { if (col < 9) { SolveSolution(Origin, Solution, row, col + 1, SolutionCount); } else { SolveSolution(Origin, Solution, row + 1, 1, SolutionCount); } } } return(Solution); }
static bool isSafe(List <Coordinate_Sudoku> Origin, List <Coordinate_Sudoku> Solution, Coordinate_Sudoku cs) { List <Coordinate_Sudoku> temp1 = Origin.Where(c => c.Value == cs.Value).ToList(); List <Coordinate_Sudoku> temp2 = Solution.Where(c => c.Value == cs.Value).ToList(); foreach (Coordinate_Sudoku c in temp1) { if (c.Row == cs.Row || c.Column == cs.Column || c.Block == cs.Block) { return(false); } } foreach (Coordinate_Sudoku c in temp2) { if (c.Row == cs.Row || c.Column == cs.Column || c.Block == cs.Block) { return(false); } } return(true); }