Пример #1
0
        public Sudoku ResoudreSudoku(Sudoku s)
        {
            var internalRows = BuildInternalRowsForGrid(s);
            var dlxRows      = BuildDlxRows(internalRows);
            var solutions    = new Dlx()
                               .Solve(dlxRows, d => d, r => r)
                               .Where(solution => VerifySolution(internalRows, solution))
                               .ToImmutableList();

            Console.WriteLine();

            if (solutions.Any())
            {
                Console.WriteLine($"First solution (of {solutions.Count}):");
                Console.WriteLine();
                DrawSolution(internalRows, solutions.First());
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No solutions found!");
            }
            Console.Read();
            return(s);
        }
Пример #2
0
        public Sudoku.Core.Sudoku Solve(Sudoku.Core.Sudoku s)
        {
            var strGrid = new List <string>(9);

            for (int i = 0; i < 9; i++)
            {
                var i1 = i;
                strGrid.Add(string.Join("", Indices.Select(j => s.Cells[i1 * 9 + j]).Select(i2 => i2 == 0 ? " " : i2.ToString(CultureInfo.InvariantCulture))));
            }
            var grid = new Grid(ImmutableList.Create(strGrid.ToArray()));

            /*int[,] mySudoku = new int[9, 9];
             * for (int i = 0; i < 9; i++) {
             *  for (int j = 0; j < 9; j++) {
             *      mySudoku[i, j] = mySudoku[i, s.Cells[j]];
             *  }
             * }
             */
            //var internalRows = BuildInternalRowsForSudoku(mySudoku);
            var internalRows = BuildInternalRowsForSudoku(grid);
            var dlxRows      = BuildDlxRows(internalRows);
            var solutions    = new Dlx()
                               .Solve(dlxRows, d => d, r => r)
                               .Where(solution => VerifySolution(internalRows, solution))
                               .ToImmutableList();

            //return SolutionToGrid(internalRows, solutions.First());
            return(SolutionToSudoku(internalRows, solutions.First(), s));
        }
Пример #3
0
        //Création d'une méthode qui prend en entrée un string (le sudoku non résolu) et qui renvoie un string (le sudoku résolu).
        //De base le code duquel on s'est inspiré ne renvoyait rien, la sortie était sous forme d'un Console.WriteLine()
        private static string Sudokusolution(string sudoku)
        {
            //Récupération du sudoku à résoudre depuis le string en entrée et transfert dans une ImmutableList.
            var grid = new Grid(ImmutableList.Create(
                                    sudoku.Substring(0, 9),
                                    sudoku.Substring(9, 9),
                                    sudoku.Substring(18, 9),
                                    sudoku.Substring(27, 9),
                                    sudoku.Substring(36, 9),
                                    sudoku.Substring(45, 9),
                                    sudoku.Substring(54, 9),
                                    sudoku.Substring(63, 9),
                                    sudoku.Substring(72, 9)));

            var internalRows = BuildInternalRowsForGrid(grid);
            var dlxRows      = BuildDlxRows(internalRows);

            var solutions = new Dlx()
                            .Solve(BuildDlxRows(internalRows), d => d, r => r)
                            .Where(solution => VerifySolution(internalRows, solution))
                            .ToImmutableList();

            if (solutions.Any())
            {
                //Enlever commentaire pour afficher les solutions dans la console

                //Console.WriteLine($"First solution (of {solutions.Count}):");
                //Console.WriteLine();
                //SolutionToGrid(internalRows, solutions.First()).Draw();
                //Console.WriteLine();

                //Ajout de ce bout de code pour avoir une sortie de type string contenant le sudoku résolu.
                string s = "";
                for (int i = 0; i <= 8; i++)
                {
                    for (int j = 0; j <= 8; j++)
                    {
                        s += SolutionToGrid(internalRows, solutions.First()).ValueAt(i, j).ToString();
                    }
                }

                return(s);
            }
            else
            {
                //Console.WriteLine("No solutions found!");
                return("No solutions found!");
            }
        }
Пример #4
0
        public void Solve(GrilleSudoku grid)
        {
            var internalRows = BuildInternalRowsForGrid(grid);
            var dlxRows      = BuildDlxRows(internalRows);
            var solutions    = new Dlx()
                               .Solve(dlxRows, d => d, r => r)
                               .Where(solution => VerifySolution(internalRows, solution))
                               .ToImmutableList();

            Console.WriteLine();

            if (solutions.Any())
            {
                Console.WriteLine($"First solution (of {solutions.Count}):");
                Console.WriteLine();
                SolutionToGrid(grid, internalRows, solutions.First());
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No solutions found!");
            }
        }
Пример #5
0
        private static void Main()
        {
            // http://puzzles.telegraph.co.uk/site/search_puzzle_number?id=27744
            var grid = new Grid(ImmutableList.Create(
                "6 4 9 7 3",
                "  3    6 ",
                "       18",
                "   18   9",
                "     43  ",
                "7   39   ",
                " 7       ",
                " 4    8  ",
                "9 8 6 4 5"));

            grid.Draw();

            var internalRows = BuildInternalRowsForGrid(grid);
            var dlxRows = BuildDlxRows(internalRows);
            var solutions = new Dlx()
                .Solve(dlxRows, d => d, r => r)
                .Where(solution => VerifySolution(internalRows, solution))
                .ToImmutableList();

            Console.WriteLine();

            if (solutions.Any())
            {
                Console.WriteLine($"First solution (of {solutions.Count}):");
                Console.WriteLine();
                DrawSolution(internalRows, solutions.First());
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No solutions found!");
            }
        }