예제 #1
0
        public void Test_SolveBoard_Solved()
        {
            int[,] array = new int[9, 9]
            {
                { 0, 0, 9, 0, 0, 0, 0, 0, 0, },
                { 3, 0, 4, 0, 8, 1, 0, 2, 0, },
                { 0, 8, 6, 5, 0, 0, 0, 0, 0, },
                { 9, 0, 1, 3, 0, 0, 0, 0, 2, },
                { 0, 0, 0, 0, 4, 0, 0, 0, 0, },
                { 6, 0, 0, 0, 0, 2, 4, 0, 1 },
                { 0, 0, 0, 0, 0, 3, 2, 6, 0 },
                { 0, 3, 0, 4, 1, 0, 5, 0, 8 },
                { 0, 0, 0, 0, 0, 0, 1, 0, 0, },
            };

            SudokoBoard   board  = SudokoBoard.CreateBoard(array);
            ISudokoSolver solver = new SudokoSolver(board);

            solver.SolveBoard();
            board = solver.GetBoard();

            bool isSolved = SudokuHelper.IsBoardSolved(board);

            Assert.IsTrue(isSolved);
        }
예제 #2
0
        static void Main(string[] args)
        {
            //int[,] array = new int[9, 9]
            //{
            //    {6, 5, 0, 0, 0, 0, 0, 9, 7},
            //    {0, 0, 4, 0, 2, 0, 0, 0, 0},
            //    {0, 0, 0, 6, 5, 7, 4, 0, 0},
            //    {0, 9, 0, 8, 0, 0, 0, 2, 3},
            //    {0, 0, 6, 3, 0, 9, 1, 0, 0},
            //    {3, 1, 0, 0, 0, 2, 0, 7, 0},
            //    {0, 0, 3, 2, 8, 4, 0, 0, 0},
            //    {0, 0, 0, 0, 9, 0, 8, 0, 0},
            //    {9, 8, 0, 0, 0, 0, 0, 4, 1},
            //};

            int[,] array = new int[9, 9]
            {
                { 0, 0, 9, 0, 0, 0, 0, 0, 0, },
                { 3, 0, 4, 0, 8, 1, 0, 2, 0, },
                { 0, 8, 6, 5, 0, 0, 0, 0, 0, },
                { 9, 0, 1, 3, 0, 0, 0, 0, 2, },
                { 0, 0, 0, 0, 4, 0, 0, 0, 0, },
                { 6, 0, 0, 0, 0, 2, 4, 0, 1 },
                { 0, 0, 0, 0, 0, 3, 2, 6, 0 },
                { 0, 3, 0, 4, 1, 0, 5, 0, 8 },
                { 0, 0, 0, 0, 0, 0, 1, 0, 0, },
            };

            SudokoBoard board = SudokoBoard.CreateBoard(array);

            Console.WriteLine(" ************ Before solution ************ ");
            Console.WriteLine();
            PrintBoard(board);
            //PrintRows(board);
            //PrintColumns(board);

            if (SudokuHelper.IsBoardValid(board))
            {
                ISudokoSolver solver = new SudokoSolver(board);
                solver.SolveBoard();

                board = solver.GetBoard();

                Console.WriteLine();

                Console.WriteLine(" ************ After solution ************ ");
                Console.WriteLine();
                PrintBoard(board);

                //board.PrintPossibleValues(false);

                Console.WriteLine();
            }
            else
            {
                //throw new InvalidOperationException("The sudoku board has some invalid cells");
                Console.WriteLine("Error: The sudoku board has some invalid cells");
            }


            List <int> lstA = new List <int> {
                1, 2, 3, 4,
            };
            List <int> lstB = new List <int> {
                4, 3, 2, 1,
            };


            var dups     = lstA.Intersect(lstB).ToList();
            var distinct = lstA.Except(lstB).ToList();

            Console.Read();
        }