public LatinSquareFC(int size, ValueMode valueMode, VariableMode variableMode, bool firstOnly = false) { _size = size; _valueMode = valueMode; _variableMode = variableMode; _firstOnly = firstOnly; board = new int[_size, _size]; _domain = _size; processed = new List <Position2>(); notProcessed = new List <Position2>(); Solutions = new List <int[, ]>(); for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { var pos = new Position2() { X = i, Y = j, Domain = new SortedSet <int>() }; for (int x = 1; x <= _domain; x++) { pos.Domain.Add(x); } pos.Removed = new List <Position2>(); notProcessed.Add(pos); } } }
private bool IsVariableInConflict(Position2 currentVariable, int currentValue) { for (int i = 0; i < _size; i++) { if (currentVariable.Y != i && board[currentVariable.X, i] == currentValue || currentVariable.X != i && board[i, currentVariable.Y] == currentValue) { return(true); } } return(false); }
private void Unfilter(Position2 variable) { var value = board[variable.X, variable.Y]; //foreach (var position2 in notProcessed.Where(np => np.X == variable.X || np.Y == variable.Y)) //{ // position2.Domain.Add(value); //} foreach (var position2 in variable.Removed) { position2.Domain.Add(value); } variable.Removed.Clear(); }
private bool Process(Position2 variable) { notProcessed.Remove(variable); processed.Add(variable); if (!notProcessed.Any()) { if (first) { watch.Stop(); TimeOfOneSolution = watch.ElapsedMilliseconds; first = false; } SaveSolution(); notProcessed.Insert(0, variable); processed.Remove(variable); //board[variable.X, variable.Y] = 0; return(true); } if (!FilterRemainingVariables(variable)) { Unfilter(variable); notProcessed.Insert(0, variable); processed.Remove(variable); return(false); } var nextVariable = GetNextVariable(); int nextValue = GetNextValue(nextVariable); while (nextValue != (int)Value.NoValue) { board[nextVariable.X, nextVariable.Y] = nextValue; Process(nextVariable); nextValue = GetNextValue(nextVariable); } Unfilter(variable); notProcessed.Insert(0, variable); processed.Remove(variable); board[nextVariable.X, nextVariable.Y] = 0; return(false); }
private bool FilterRemainingVariables(Position2 variable) { var value = board[variable.X, variable.Y]; foreach (var position2 in notProcessed.Where(np => np.X == variable.X || np.Y == variable.Y)) { var b = position2.Domain.Remove(value); if (b) { variable.Removed.Add(position2); } if (!position2.Domain.Any()) { return(false); } } return(true); }
private int GetNextValue(Position2 currentVariable) { var next = currentVariable.Domain.FirstOrDefault(x => x > board[currentVariable.X, currentVariable.Y]); return(next == 0 ? (int)Value.NoValue : next); }
private Position2 GetPreviousVariable(Position2 curentVaiable) { board[curentVaiable.X, curentVaiable.Y] = 0; return(processed.Last()); }
private void StepBack(Position2 currentVariable) { notProcessed.Insert(0, currentVariable); processed.Remove(processed.Last()); }