示例#1
0
 private static SolveStats _TryAllSolutions(SquareTracker <TPuzzle> tracker)
 {
     if (tracker.IsSolved)
     {
         return(new SolveStats()
         {
             NumSolutionsFound = 1
         });
     }
     (Coordinate c, int[]? possibleValues) = tracker.GetBestGuess();
     if (possibleValues.Length == 1)
     {
         if (tracker.TrySet(in c, possibleValues[0]))
         {
             return(_TryAllSolutions(tracker));
         }
         return(new SolveStats());
     }
     return(_TryAllSolutionsWithGuess(in c, possibleValues, tracker));
 }
示例#2
0
 private static bool _TrySolve(SquareTracker <TPuzzle> tracker)
 {
     if (tracker.IsSolved)
     {
         return(true);
     }
     (Coordinate c, int[]? possibleValues) = tracker.GetBestGuess();
     foreach (int possibleValue in possibleValues)
     {
         if (tracker.TrySet(in c, possibleValue))
         {
             if (_TrySolve(tracker))
             {
                 return(true);
             }
             tracker.UnsetLast();
         }
     }
     return(false);
 }
示例#3
0
        private static bool _TrySolveRandomly(Random rand, SquareTracker <TPuzzle> tracker)
        {
            if (tracker.IsSolved)
            {
                return(true);
            }
            (Coordinate c, int[]? possibleValues) = tracker.GetBestGuess();
            var possibleValuesList = new List <int>(possibleValues);

            while (possibleValuesList.Count > 0)
            {
                int possibleValue = possibleValuesList[rand.Next(0, possibleValuesList.Count)];
                if (tracker.TrySet(in c, possibleValue))
                {
                    if (_TrySolveRandomly(rand, tracker))
                    {
                        return(true);
                    }
                    tracker.UnsetLast();
                }
                _ = possibleValuesList.Remove(possibleValue);
            }
            return(false);
        }