示例#1
0
        private static bool SolveWithStyle(Board board)
        {
            board.Print();
            Console.Out.WriteLine();
            int[] gridSpace;
            if ((gridSpace = FindNextZero(board)) == null)
            {
                return(true);
            }

            for (int i = 1; i <= 9; i++)
            {
                board.grid[gridSpace[0], gridSpace[1]].setValue(i);
                if (board.CheckGridSpace(gridSpace) == true)
                {
                    if (SolveWithStyle(board) == true)
                    {
                        return(true);
                    }
                }
            }

            board.grid[gridSpace[0], gridSpace[1]].setValue(0);

            return(false);
        }
示例#2
0
        public void SolveSudoku(char[][] source)
        {
            var board = new Board(source);

            board.Fill(print: false);
            board.Print(leaveInTheEnd: true);
        }
示例#3
0
        static void Main()
        {
            //int[,] state = new int[9, 9]
            //{
            //    {5, 3, 0, 0, 7, 0, 0, 0, 0},
            //    {6, 0, 0, 1, 9, 5, 0, 0, 0},
            //    {0, 9, 8, 0, 0, 0, 0, 6, 0},
            //    {8, 0, 0, 0, 6, 0, 0, 0, 3},
            //    {4, 0, 0, 8, 0, 3, 0, 0, 1},
            //    {7, 0, 0, 0, 2, 0, 0, 0, 6},
            //    {0, 6, 0, 0, 0, 0, 2, 8, 0},
            //    {0, 0, 0, 4, 1, 9, 0, 0, 5},
            //    {0, 0, 0, 0, 8, 0, 0, 7, 9}
            //};
            int[,] state = new int[9, 9]
            {
                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 1, 0, 0, 2, 0, 0, 3, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },

                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 4, 0, 0, 5, 0, 0, 6, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },

                { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 7, 0, 0, 8, 0, 0, 9, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            };
            Board board = new Board(state);

            board.Print();

            //board.getSpace(x, x).addDependency(board.getSpace(x, x), x);

            board.getSpace(0, 0).addDependency(board.getSpace(1, 0), 1);
            board.getSpace(1, 0).addDependency(board.getSpace(0, 0), 1);

            board.getSpace(0, 2).addDependency(board.getSpace(0, 3), 1);
            board.getSpace(0, 3).addDependency(board.getSpace(0, 2), 1);

            board.getSpace(0, 7).addDependency(board.getSpace(1, 7), 1);
            board.getSpace(1, 7).addDependency(board.getSpace(0, 7), 1);

            board.getSpace(3, 0).addDependency(board.getSpace(4, 0), 1);
            board.getSpace(4, 0).addDependency(board.getSpace(3, 0), 1);

            board.getSpace(4, 3).addDependency(board.getSpace(5, 3), 1);
            board.getSpace(5, 3).addDependency(board.getSpace(4, 3), 1);

            board.getSpace(5, 6).addDependency(board.getSpace(5, 7), 1);
            board.getSpace(5, 7).addDependency(board.getSpace(5, 6), 1);

            board.getSpace(6, 0).addDependency(board.getSpace(7, 0), 2);
            board.getSpace(7, 0).addDependency(board.getSpace(6, 0), 2);

            board.getSpace(6, 8).addDependency(board.getSpace(7, 8), 2);
            board.getSpace(7, 8).addDependency(board.getSpace(6, 8), 2);
            //Ekat

            board.getSpace(0, 6).addDependency(board.getSpace(1, 6), 1);
            board.getSpace(1, 6).addDependency(board.getSpace(0, 6), 1);
            board.getSpace(1, 6).addDependency(board.getSpace(1, 5), 1);
            board.getSpace(1, 5).addDependency(board.getSpace(1, 6), 1);
            board.getSpace(1, 5).addDependency(board.getSpace(2, 5), 1);
            board.getSpace(2, 5).addDependency(board.getSpace(1, 5), 1);

            board.getSpace(1, 2).addDependency(board.getSpace(1, 3), 1);
            board.getSpace(1, 3).addDependency(board.getSpace(1, 2), 1);
            board.getSpace(1, 3).addDependency(board.getSpace(2, 3), 2);
            board.getSpace(2, 3).addDependency(board.getSpace(1, 3), 2);

            board.getSpace(2, 1).addDependency(board.getSpace(2, 2), 2);
            board.getSpace(2, 2).addDependency(board.getSpace(2, 1), 2);
            board.getSpace(2, 2).addDependency(board.getSpace(3, 2), 2);
            board.getSpace(3, 2).addDependency(board.getSpace(2, 2), 2);

            board.getSpace(5, 1).addDependency(board.getSpace(5, 2), 1);
            board.getSpace(5, 2).addDependency(board.getSpace(5, 1), 1);
            board.getSpace(5, 2).addDependency(board.getSpace(6, 2), 1);
            board.getSpace(6, 2).addDependency(board.getSpace(5, 2), 1);
            board.getSpace(6, 2).addDependency(board.getSpace(6, 1), 2);
            board.getSpace(6, 1).addDependency(board.getSpace(6, 2), 2);

            board.getSpace(7, 3).addDependency(board.getSpace(8, 3), 1);
            board.getSpace(8, 3).addDependency(board.getSpace(7, 3), 1);
            board.getSpace(8, 3).addDependency(board.getSpace(8, 4), 2);
            board.getSpace(8, 4).addDependency(board.getSpace(8, 3), 2);
            board.getSpace(8, 4).addDependency(board.getSpace(8, 5), 1);
            board.getSpace(8, 5).addDependency(board.getSpace(8, 4), 1);
            board.getSpace(8, 5).addDependency(board.getSpace(7, 5), 2);
            board.getSpace(7, 5).addDependency(board.getSpace(8, 5), 2);
            board.getSpace(7, 5).addDependency(board.getSpace(7, 6), 2);
            board.getSpace(7, 6).addDependency(board.getSpace(7, 5), 2);
            board.getSpace(7, 6).addDependency(board.getSpace(6, 6), 2);
            board.getSpace(6, 6).addDependency(board.getSpace(7, 6), 2);
            board.getSpace(6, 6).addDependency(board.getSpace(6, 5), 1);
            board.getSpace(6, 5).addDependency(board.getSpace(6, 6), 1);

            board.getSpace(3, 3).addDependency(board.getSpace(3, 4), 1);
            board.getSpace(3, 4).addDependency(board.getSpace(3, 3), 1);
            board.getSpace(3, 4).addDependency(board.getSpace(4, 4), 1);
            board.getSpace(4, 4).addDependency(board.getSpace(3, 4), 1);
            board.getSpace(4, 4).addDependency(board.getSpace(5, 4), 1);
            board.getSpace(5, 4).addDependency(board.getSpace(4, 4), 1);

            board.getSpace(3, 8).addDependency(board.getSpace(4, 8), 2);
            board.getSpace(4, 8).addDependency(board.getSpace(3, 8), 2);
            board.getSpace(4, 8).addDependency(board.getSpace(5, 8), 1);
            board.getSpace(5, 8).addDependency(board.getSpace(4, 8), 1);

            //TODO testaa matriisin suunnat, hardkoodaa dependencies, pitäisi toimia?
            //board.getSpace(1, 0).setValue(10);
            //board.getSpace(0, 1).setValue(01);
            // 0 1
            // 10 0

            Solve(board);
            Console.Out.WriteLine();
            board.Print();

            //solveWithStyle(board);
        }