예제 #1
0
 public static void EliminateDuplicates(SudokuModel model, int col, int row)
 {
     if (model.IsSolved(col, row)) {
         var value = model.GetValue(col, row);
         foreach (var region in model.GetIntersectingRegions(col, row)) {
             foreach (var other in model.GetCells(region.Type, region.I)) {
                 if (other.Column != col || other.Row != row) {
                     model.Eliminate(other.Column, other.Row, value);
                 }
             }
         }
     }
 }
예제 #2
0
        /// <summary>
        ///   Copy constructor
        /// </summary>
        /// <param name = "model"></param>
        public SudokuModel(SudokuModel model)
        {
            // Initialize our variables
            var size = model.Size;
            _size = size;
            _sizeSqrt = Convert.ToInt32(Math.Sqrt(size));
            _sizeSquared = size * size;
            _sizeCubed = _sizeSquared * size;
            _possibilitySetModel = (1 << _size) - 1;
            _possibilitySetCell = new int[Size, Size];
            _remainingPossibilityCount = new int[Size, Size];
            _value = new int[Size, Size];
            _lastChangedIterCell = new int[Size, Size];

            // Initialize the Cell objects, and the cache values
            for (var col = 0; col < size; ++col) {
                for (var row = 0; row < size; ++row) {
                    _possibilitySetCell[col, row] = model.GetPossibilitySetCell(col, row);
                    _remainingPossibilityCount[col, row] = model.GetRemainingPossibilityCount(col, row);
                    _value[col, row] = model.GetValue(col, row);
                    _lastChangedIterCell[col, row] = model.GetLastChangedIterCell(col, row);
                    CellChanged = null;
                }
            }
            CellChanged += HandleCellChanged;

            _solved = new int[3, _size];
            _lastChangedIterRegion = new int[3, _size];
            _cellsRegion = new Cell[3, _size][];
            _intersectingRegions = new Region[Size, Size][];
            for (var i = 0; i < _size; ++i) {
                _lastChangedIterRegion[0, i] = model._lastChangedIterRegion[0, i];
                _lastChangedIterRegion[1, i] = model._lastChangedIterRegion[0, i];
                _lastChangedIterRegion[2, i] = model._lastChangedIterRegion[0, i];
                SetLastChangedIterRegion(RegionType.Column, i, -1);
                SetLastChangedIterRegion(RegionType.Row, i, -1);
                SetLastChangedIterRegion(RegionType.Square, i, -1);
                _solved[0, i] = model._solved[0, i];
                _solved[1, i] = model._solved[1, i];
                _solved[2, i] = model._solved[2, i];
                var colCells = new Cell[_size];
                for (var j = 0; j < _size; ++j) {
                    colCells[j] = new Cell(i, j);
                }
                _cellsRegion[(int)RegionType.Column, i] = colCells;
                var rowCells = new Cell[_size];
                for (var j = 0; j < _size; ++j) {
                    rowCells[j] = new Cell(j, i);
                }
                _cellsRegion[(int)RegionType.Row, i] = rowCells;
                var sqCells = new Cell[_size];
                var sqrCol = i % _sizeSqrt;
                var sqrRow = i / _sizeSqrt;
                var startCol = sqrCol * _sizeSqrt;
                var startRow = sqrRow * _sizeSqrt;
                for (var c = 0; c < _sizeSqrt; ++c) {
                    for (var r = 0; r < _sizeSqrt; ++r) {
                        sqCells[r * _sizeSqrt + c] = new Cell(c + startCol, r + startRow);
                    }
                }
                _cellsRegion[(int)RegionType.Square, i] = sqCells;
            }
            SolvedCount = model.SolvedCount;
            EliminatedCount = model.EliminatedCount;
            ChangeCount = model.ChangeCount;
            LastChangedCell = model.LastChangedCell;
        }