public void Solve() { BasicSolve(); if (IsLegal && !IsSolved) { var undeterminedCells = MatrixIterator .Where(cell => !cell.IsDetermined); SudokuCell speculationTarget = undeterminedCells .First(cell => cell.Candidates.Count == undeterminedCells.Min(c => c.Candidates.Count)); Random rng = new Random(); var shuffledCandidates = speculationTarget.Candidates .OrderBy(c => rng.Next()); foreach (int candidate in shuffledCandidates) { SudokuBoard speculativeBoard = new SudokuBoard(this); speculativeBoard.SetCell( cellRow: speculationTarget.Row, cellColumn: speculationTarget.Column, valueToSet: candidate); speculativeBoard.Solve(); if (speculativeBoard.IsLegal && speculativeBoard.IsSolved) { this.Copy(speculativeBoard); return; } } } }
public async Task ParallelSolve(int branchingDepth = -1) { if (branchingDepth == 0) { Solve(); return; } BasicSolve(); if (IsLegal && !IsSolved) { var undeterminedCells = MatrixIterator .Where(cell => !cell.IsDetermined); SudokuCell speculationTarget = undeterminedCells .First(cell => cell.Candidates.Count == undeterminedCells.Min(c => c.Candidates.Count)); var tokenSource = new CancellationTokenSource(); List <Task <SudokuBoard> > speculationTasks = new List <Task <SudokuBoard> >(); foreach (int candidate in speculationTarget.Candidates) { SudokuBoard speculativeBoard = new SudokuBoard(this); speculativeBoard.SetCell( cellRow: speculationTarget.Row, cellColumn: speculationTarget.Column, valueToSet: candidate); var token = tokenSource.Token; speculationTasks.Add( Task.Factory.StartNew(() => { speculativeBoard .ParallelSolve(branchingDepth > 0 ? branchingDepth - 1 : branchingDepth) .Wait(token); return(speculativeBoard); }, token) ); } while (speculationTasks.Count > 0) { Task <SudokuBoard> completedTask = await Task.WhenAny(speculationTasks); SudokuBoard speculativeBoard = completedTask.Result; if (speculativeBoard.IsLegal && speculativeBoard.IsSolved) { tokenSource.Cancel(); this.Copy(speculativeBoard); return; } speculationTasks.Remove(completedTask); } } }
static void Main() { var array = new int[] { 1, 2, 3, 4 }; var matrix = new int[, ] { { 5, 6, 7 }, { 8, 9, 10 } }; var arrayIterator = new ArrayIterator(array); var matrixIterator = new MatrixIterator(matrix); Print(arrayIterator); Print(matrixIterator); }
public void RecomputeCandidates() { MatrixIterator .Where(cell => !cell.IsDetermined) .ToList() .ForEach(cell => cell.Clear()); MatrixIterator .Where(cell => cell.IsDetermined) .ToList() .ForEach(cell => UpdateBoardCandidates( sourceRow: cell.Row, sourceColumn: cell.Column, valueToRemove: (int)cell.Value)); }
static void Main(string[] args) { Iterator <int> collection1 = new LinkedListIterator <int>(new Node <int>(1, new Node <int>(2, new Node <int>(3, new Empty <int>())))); PrintCollection(collection1); Iterator <int> collection2 = new ArrayIterator <int>(new int[] { 9, 10, 11, -1 }); PrintCollection(collection2); int[][] tmp_collection3 = new int[3][]; tmp_collection3[0] = new int[] { 3, 2, 1 }; tmp_collection3[1] = new int[] { 2, 1, 3 }; tmp_collection3[2] = new int[] { 1, 3, 2 }; Iterator <int> collection3 = new MatrixIterator <int>(tmp_collection3, 3, 3); PrintCollection(collection3); Iterator <int> collection4 = new NaturalNumbers(); PrintCollection(collection4); }
public int SetNakedCandidateCells() { int nCellsSet = 0; MatrixIterator .Where(cell => cell.HasNakedCandidate()) .ToList() .ForEach( cell => { if (cell.Candidates.Count == 1) { SetCell( cellRow: cell.Row, cellColumn: cell.Column, valueToSet: cell.Candidates.First()); nCellsSet++; } }); return(nCellsSet); }