private static SudokuPuzzle Backtracking(SudokuPuzzle puzzle) { if (puzzle.IsComplete) { return(puzzle); } if (_stopwatch.Elapsed.Minutes >= 5) { return(null); } var cell = puzzle.NextCell(); // Keep track of first three steps if (Steps.Count < 3) { Steps.Add(new SudokuPuzzleStepModel { Degree = puzzle.GetDegree(cell), DomainSize = cell.Domain.Count, SelectedCol = cell.Col, SelectedRow = cell.Row }); } foreach (var value in cell.Domain) { // Generate next node var clone = puzzle.Clone(); clone.SetCellValue(cell.Row, cell.Col, value); // If there exists a cell with an empty domain after forward checking, backtrack if (clone.MinRemainingValue()?.Domain.Count == 0) { continue; } var result = Backtracking(clone); if (result != null) { return(result); } } return(null); }