Пример #1
0
        private SolutionInfo CheckCellValuesForViability(OpenCell cell)
        {
//            debugLog.Write("CheckCellValuesForViability(" + cell.ToString() + ")");
            SolutionInfo solutionInfo      = new SolutionInfo();
            int          sumOfUnsolvedCels = SumOfAllUnsolvedCells();

            if (sumOfUnsolvedCels == 0)
            {
                solutionInfo.solvable = false;
                return(solutionInfo);
            }
            List <int>      possibleDigits         = new List <int>(cell.GetPossibleValues());
            int             initialNumberOfDigits  = cell.GetPossibleValues().Count;
            List <OpenCell> copyOfUnsolvedCellList = data.GetCopyOfUnsolvedCells();

            copyOfUnsolvedCellList.Remove(cell);
            Stack <OpenCell> cellStack = new Stack <OpenCell>(copyOfUnsolvedCellList);

            foreach (int digit in possibleDigits)
            {
                if (digit <= sumOfUnsolvedCels)
                {
                    List <int> availableDigitsFromGroup = new List <int>(data.availableDigits);
                    availableDigitsFromGroup.Remove(digit);
                    if (!SumWorksForGroup(sumOfUnsolvedCels - digit, availableDigitsFromGroup, cellStack))
                    {
                        cell.DigitIsNotAPossibleValue(digit);
                    }
                }
                else
                {
                    cell.DigitIsNotAPossibleValue(digit);
                }
            }
            int finalNumberOfDigits = cell.GetPossibleValues().Count;

            solutionInfo.increaseInNumber = finalNumberOfDigits - initialNumberOfDigits;
            debugLog.Write("        Validate cell " + cell.ToString() +
                           "(start:" + initialNumberOfDigits + " end:" + finalNumberOfDigits + ")\n");
            if (finalNumberOfDigits == 0)
            {
                solutionInfo.solvable = false;
            }
            return(solutionInfo);
        }
Пример #2
0
        private bool SolveForLastCell()
        {
            debugLog.Write("Only one cell to try.  Solving that cell...\n");
            OpenCell cellToSolve = data.GetCopyOfUnsolvedCells().ElementAt(0);
            int      randomValue = cellToSolve.GetARandomValue();

            if (randomValue == 0)
            {
                return(false);
            }
            int sum = SumOfAllSolvedCells() + randomValue;

            if (data.unusableSums.Contains(sum))
            {
                cellToSolve.DigitIsNotAPossibleValue(randomValue);
                return(SolveForLastCell());
            }
            cellToSolve.SetToAValue(randomValue);
            FinalizeBothParents(cellToSolve);
            return(UpdateSum(sum));  // Need to deal with a failure here...
        }