private void addOneEntry(LatinSquare square) { try { while (solution == null) { square.AddFindings(); var target = findEntryWithMinChoices(square); if (target.Row == -1) { solution = square; tasks.AbortAll(); return; } List <int> values = square.entries[target.Row][target.Col].GetAllowedValues(); if (Reverse) { values.Reverse(); } int remaining = values.Count; foreach (int num in values) { if (--remaining > 0) { try { LatinSquare candidate = square.CreateCopy(); candidate.SetEntry(target.Row, target.Col, num); tasks.Add(candidate); } catch (IllegalEntryException) { Effort++; } } else { square.SetEntry(target.Row, target.Col, num); // just continue loop instead of wait for another task } } } } catch (IllegalEntryException) { Effort++; } }
/** * Specifies an entry value which might have various implications abaut the other entries. */ public virtual void SetEntry(int row, int col, int num) { if (entries[row][col].SetValue(num) == NumberSet.ALREADYKNOWN) { return; } nextTransposition.SetEntry(col, num, row); for (int i = 0; i < entries.Length; i++) { if (i != row) { forbidValue(i, col, num); } if (i != col) { forbidValue(row, i, num); } } }
public LatinSquare Complete(LatinSquare square) { if (square == null) { throw new ArgumentNullException(nameof(square)); } square.AddFindings(); var target = findEntryWithMinChoices(square); if (target.Row == -1) { return(square); } IllegalEntryException last = null; List <int> values = square.entries[target.Row][target.Col].GetAllowedValues(); if (Reverse) { values.Reverse(); } int remaining = values.Count; foreach (int num in values) { try { LatinSquare candidate = (--remaining == 0 ? square : square.CreateCopy()); candidate.SetEntry(target.Row, target.Col, num); return(Complete(candidate)); } catch (IllegalEntryException e) { Effort++; last = e; } } throw last; }