예제 #1
0
        public byte[,] GeneratePuzzleWithClues(int n)
        {
            if (n < 17)
            {
                Console.WriteLine("Not possible!");
            }
            var exactCover = new ExactCover();

            byte[,] completedSudoku;
            byte[,] cluesRemoved;

            int attempts = 0;

            do
            {
                completedSudoku = GenerateRandomCompleteGrid();
                cluesRemoved    = RemoveClues(completedSudoku, 81 - n);
                attempts++;
            }while (!exactCover.CheckExactlyOneSolution(GenerateExactCoverProblem(cluesRemoved)));

            Console.WriteLine("Attempts: " + attempts);

            Print(completedSudoku);
            Console.WriteLine("#########");
            Print(cluesRemoved);

            return(cluesRemoved);
        }
예제 #2
0
 public override void OnUncover(ExactCover ec)
 {
     if (cageOptional != null)
     {
         cageOptional.OnUncover(ec, this);
     }
 }
예제 #3
0
        public void OnCover(ExactCover ec, SudokuCandidate sc)
        {
            int cage = sc.c;
            int num  = sc.n + 1;

            System.Diagnostics.Debug.Assert(info.sizes[cage] > 0);
            SudokuSolver ss = (SudokuSolver)ec;

            info.remaining_totals[cage] -= num;
            info.remaining_sizes[cage]--;
            removed = CheckRemains(ss, info, cage, num);
        }
예제 #4
0
        public byte[,] GenerateRandomCompleteGrid()
        {
            byte[,] exactCoverProblem = GenerateExactCoverProblem(new byte[9, 9] {
                { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
            });
            List <int> rowsRandomOrder;

            (exactCoverProblem, rowsRandomOrder) = RandomiseMatrix(exactCoverProblem);

            var exactCover = new ExactCover();
            var result     = exactCover.GetFirstSolution(exactCoverProblem);

            return(ExactCoverResultToSudoku(result, rowsRandomOrder));
        }
예제 #5
0
        public void OnUncover(ExactCover ec, SudokuCandidate sc)
        {
            if (removed != null)
            {
                for (int i = removed.Length - 1; i >= 0; --i)
                {
                    ec.UndiscardCandidate(removed[i]);
                }
            }

            int num = sc.n + 1;

            info.remaining_totals[sc.c] += num;
            info.remaining_sizes[sc.c]++;
        }
예제 #6
0
        public byte[,] Solve(byte[,] input)
        {
            byte[,] exactCoverProblem = GenerateExactCoverProblem(input);

            var exactCover = new ExactCover();
            var results    = exactCover.GetAllSolutions(exactCoverProblem);

            if (results.Count > 1)
            {
                throw new ArgumentException("More than one solution exists.");
            }
            else
            {
                return(ExactCoverResultToSudoku(results[0]));
            }
        }