如何在Sudoku Solver中找到并解决多种解决方案?

时间:2019-03-30 06:26:23

标签: java recursion sudoku

我创建了一个程序来为数独板输入一个txt文件。我将其保存到2D数组中,并具有求解功能以对其进行求解。我唯一剩下的问题是,一旦找到解决方案,我将如何添加查找另一种解决方案的能力?找到解决方案后,我会问用户三个选项。如果他们想找到其他解决方案,请在电路板上更改一个值,然后退出。

下面是二维数组,它是木板,0表示空白

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

下面是解决板子的功能。虽然这足以解决董事会的问题。解决其他时间找到另一个解决方案是行不通的。我需要用户输入的选项A。

private static boolean isRowAvailable(int[][] board, int coordinateX, int value){
        for(int i = 0; i < 9; i++){
            if(board[coordinateX][i] == value)
                return false;
        }
        return true;
    }

    private static boolean isColumnAvailable(int[][] board, int coordinateY, int value){
        for(int i = 0; i < 9; i++){
            if(board[i][coordinateY] == value)
                return false;
        }
        return true;
    }

    private static boolean isSquareAvailable(int[][] board, int coordinateX, int coordinateY, int value){
        int firstNewX = 0, secondNewX = 0, firstNewY = 0, secondNewY = 0;

        if(coordinateX >= 0 && coordinateX <= 2){
            firstNewX = 0;
            secondNewX = 2;
        }
        else if(coordinateX >= 3 && coordinateX <= 5){
            firstNewX = 3;
            secondNewX = 5;
        }
        else if(coordinateX >= 6 && coordinateX <= 8){
            firstNewX = 6;
            secondNewX = 8;
        }

        if(coordinateY >= 0 && coordinateY <= 2){
            firstNewY = 0;
            secondNewY = 2;
        }
        else if(coordinateY >= 3 && coordinateY <= 5){
            firstNewY = 3;
            secondNewY = 5;
        }
        else if(coordinateY >= 6 && coordinateY <= 8){
            firstNewY = 6;
            secondNewY = 8;
        }

        for(int i = firstNewX; i <= secondNewX; i++){
            for(int j = firstNewY; j <= secondNewY; j++){
                if(board[i][j] == value)
                    return false;
            }
        }
        return true;

    }

    private static boolean isAvailable(int[][] board, int coordinateX, int coordinateY, int value){
        if(isRowAvailable(board, coordinateX, value) &&
                isColumnAvailable(board, coordinateY, value) &&
                isSquareAvailable(board, coordinateX, coordinateY, value) &&
                board[coordinateX][coordinateY] == 0)
            return true;
        return false;
    }

    private static boolean findEmptySpot(int[][] board, int[] coordinates){
        for(coordinates[0] = 0; coordinates[0] < 9; coordinates[0]++){
            for(coordinates[1] = 0; coordinates[1] < 9; coordinates[1]++){
                if(board[coordinates[0]][coordinates[1]] == 0)
                    return true;
            }
        }
        return false;
    }

    private static boolean solveSudoku(int[][] board){
        int x = 0, y = 0;
        int[] coordinates = {x, y};

        if (!findEmptySpot(board, coordinates))
            return true;

        x = coordinates[0];
        y = coordinates[1];

        for(int num = 1; num <= 9; num++)
        {
            if (isAvailable(board, x, y, num))
            {
                board[x][y] = num;

                if(solveSudoku(board))
                    return true;

                board[x][y] = 0;
            }
        }
        return false;
    }

下面是处理用户输入菜单的功能

switch(upperResponse){
                case 'A':
                    //instructions for finding another solution
                    break;
                case 'B':
                    changeConstraint();
                    break;
                case 'C':
                    break;
                default:
                    System.out.println("Please enter valid option");
            }

我希望达到的预期结果是为此

Solving the puzzle below:
0 0 1 6 0 0 0 7 0
0 0 0 0 3 0 0 0 6
0 7 0 0 4 0 3 0 0
0 0 9 0 0 0 6 0 3
0 0 0 0 0 0 0 1 0
0 0 0 8 1 0 0 9 2
0 0 6 0 0 0 0 2 5
0 0 0 0 5 0 0 0 0
3 2 0 4 0 0 8 0 9 
A solution is:
5 3 1 6 8 2 9 7 4
8 9 4 1 3 7 2 5 6
6 7 2 5 4 9 3 8 1
1 8 9 7 2 5 6 4 3
2 5 3 9 6 4 7 1 8
4 6 7 8 1 3 5 9 2
7 1 6 3 9 8 4 2 5
9 4 8 2 5 6 1 3 7
3 2 5 4 7 1 8 6 9
What would you like to do? 
(A) find another solution, (B) change a constraint  (C) quit
Input: A
Another solution is:
5 3 1 6 8 2 9 7 4
8 9 4 1 3 7 2 5 6
6 7 2 5 4 9 3 8 1
1 8 9 7 2 5 6 4 3
2 5 3 9 6 4 7 1 8
4 6 7 8 1 3 5 9 2
7 4 6 3 9 8 1 2 5
9 1 8 2 5 6 4 3 7
3 2 5 4 7 1 8 6 9

0 个答案:

没有答案