开发者

Logic Solving Algorithm for Sudoku (Java)

I decided to write a logic solving algorithm for my Sudoku application. What I wrote works for a limited amount开发者_如何学C of grid values, but then the recursion stops way too soon.

What my methods do: addToThirdDimension(): A three dimensional array stores any possible values that can be put into the grid value at logicGrid[x][y]. This method refreshes the three dimensional array. It does this by testing values 1-9 in every grid index, and if it's valid, it adds that number to the array. If not, it sets that value to zero.

checkValues(): Checks how many possibilities are left in the three dimensional grid. It goes through the logicGrid and returns the number of non-zero values are in the grid.

checkSingleValue(int row, int col): Checks logicGrid[row][col] to see if there is one and only one value left in there (If there is one value left, it is the only possibility for the grid element at [row, col]). It returns the amount of non-zero values that are in that grid location.

getSingleValue(int row, int col): Returns the single number that's left in logicGrid[row][col]

immutableValues: A two dimensional boolean array that stores whether or not a specific grid element is immutable or not. If it is immutable, the solve method should not touch it.

public boolean solveWithLogic(){

    addToThirdDimension();
    if(checkValues() == 0){
        return true;
    }

    for(int row = 0; row < 9; row++){
        for(int col = 0; col < 9; col++){
            if(!immutableValues[row][col]){
                if(checkSingleValue(row, col) == 1){
                    sGrid[row][col] = getSingleValue(row, col);
                    setValues[row][col] = true;
                    addToThirdDimension();
                }
            }
        }
    }

    if(checkValues() != 0){
        solveWithLogic();
    } else{
        return true;
    }

    return false;
}

I cannot see where I am going wrong. After a certain number of tries, checkValues returns 0 even though there should be more possibilities. Here is the code for addToThirdDimension() as I am sure that if something is wrong, it is here.

sGrid is the main two-dimensional integer array that stores the values for the puzzle.

public void addToThirdDimension(){

    logicGrid = new int[9][9][9];

    for(int x = 0; x < 9; x++){
        for(int y = 0; y < 9; y++){
            for(int z = 0; z < 9; z++){
                logicGrid[x][y][z] = z + 1;
            }
        }
    }

    int[][] temp1 = sGrid;

    for(int row = 0; row < 9; row++){
        for(int col = 0; col < 9; col++){
            if(setValues[row][col]){
                for(int i = 0; i < 9; i++){
                    logicGrid[row][col][i] = 0;
                }
            } else{
                for(int i = 1; i <= 9; i++){
                    temp1[row][col] = i;
                    if(!isColumnValid(col, temp1) && !isRowValid(row, temp1) &&
                            !isQuadrantValid(row, col, temp1){
                        logicGrid[row][col][i-1] = 0;
                    }
                }
            }
            temp1[row][col] = sGrid[row][col];
        }
    }
}

The code isn't too efficient at the moment. I want to get it working before I start minimizing solve times.


The first thing I would do is create a SudukoCell object that stores your possible values in it. Then create a SudukoBoard with a 2d array of SudukoCells. Also give it an array of SudukoAreas. One area for rows, one area for cols, and one area for blocks.

Add your suduko cells appropriately.

This will help you consolidate your legwork and prevent silly mistakes.

then every time you solve a number, you can go to the cells in each of its areas and remove the number you solved from them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜