开发者

Finding Zeroes in Sudoku

开发者 https://www.devze.com 2023-02-15 13:58 出处:网络
I had a class assignment (it\'s already past) where I had to write a Sudoku solver. I was able to create a method that can solve for each missing number. But I\'m having trouble creating a method to f

I had a class assignment (it's already past) where I had to write a Sudoku solver. I was able to create a method that can solve for each missing number. But I'm having trouble creating a method to find which cells I need to solve. I'm supposed to take a 2D array and fill in the missing number (represented by 0's). I've put some of my code below, but not all of it (even though the assignment has passed, I respect the profs wishes).

public class SolveSudoku {

    private static int[][] grid = new int[9][9];

    public static int[][] getSolution(int[][] grid) {
        for (int i = 0; i < 9; i++) {
            System.arraycopy(grid[i], 0, SolveSudoku.grid[i], 0, 9);
        }
        int n = getZero();
        return getSolution(n);
    }

    private static int[][] getSolution(int n) {
        if (n == 0) {
            return grid;
        }
        Cell cell = getZero();
        int row = cell.row;
        int column = cell.column;
        for (int number = 1; number <开发者_运维知识库;= 9; number++) {
            //checks cell using another method
            //I have booleans that return true if the number works
        }
        return null;
    }

    private static int getZero() {
        return 0;
    }

    private static class Cell {
        int cell = 0;
        int row = 0;
        int column = 0;
        int number;
    }
}

I have the getZero method which has to find each zero in the grid (0's represent a missing number) so I can solve it. What should I do in getZero to find the cells I need to replace?


You don't check if the number is repeated in the same 3x3 square (only rows and columns).

I don't get what do you mean by "finding zeroes". In fact it is meaningless, solving the sudoku from the first row or the last one has no effect in the solution. I'll explain what did I do for the same trouble.

  1. A cell is not an int, is an object. In the object I store a value (the number if it is found, 0 if not) and a boolean[9]. False means (index + 1) is discarded because it is in the same row/column/square, true means it is not decided yet. Also, 3 Lists (Vectors, Sets, whatever).
  2. A list of 81 cells (you can make it a bidimensional array if you wish).
  3. 9 Lists/Vectors/Sets representing rows, 9 representing columns, 9 representing square. Each of the 81 cells is assigned to its row/column/square. Inside each cell you store the references to the list to which it belongs to.
  4. Now comes the execution part. In a first iteration, every time you find a non-zero (a number fixed in the sudoku), you loop through the lists to which the cell belongs. For each cell in those lists, you mark the boolean assigned to that number to false.
  5. You loop through cells, each time you find a 0 you check how many of the booleans in the cell are true. If it is 1 (all other 8 possible values have been discarded), it is the value of the cell. You set it, and as in 4) you get the list to which it belongs and mark that number to false in every cells. Loop until you get a solution, or an iteration in which you cannot set any number (no solution available directly, must start with backtracking or similar).

Remember before getting at the keyboard, to have a clear idea about what the question is and how would you resolve it without a computer. If you do not know what you want to do, the computer won't help (unless you post in stackoverflow)-


From what I can tell you want to find the first 0-valued cell in grid. I'll define first as the first zero containing column in the lowest zero-containing row.

This can be done using a naive search:

private Cell getZeroCell(){
    int rz = -1;
    int cz = -1;
    outer: for(int row = 0; row < grid.length; row++){
        for(int col = 0; col < grid[row].length; col++){
            if(grid[row][col] == 0){
               rz = row;
               cz = col;
               break outer;
            }
        }
    }
    if(rz == -1 || cz == -1){
       // no zeros found
       return null;
    }else{
       // first zero found at row `rz` and column `cz`
       Cell cell = new Cell();
       cell.row = rz;
       cell.column = cz;
       return cell;
    }
}

Get the "number" of the first cell containing a zero (counting left to right, then top to bottom, 0-indexed):

private int getZeroInt(){
    int rz = -1;
    int cz = -1;
    outer: for(int row = 0; row < grid.length; row++){
        for(int col = 0; col < grid[row].length; col++){
            if(grid[row][col] == 0){
               rz = row;
               cz = col;
               break outer;
            }
        }
    }
    if(rz == -1 || cz == -1){
       // no zeros found
       return -1;
    }else{
       return rz * grid[0].length + cz;
    }
}

Get the number of cells containing a zero:

private int getNumZeros(){
    int count = 0;
    for(int row = 0; row < grid.length; row++){
        for(int col = 0; col < grid[row].length; col++){
            if(grid[row][col] == 0){
              count++;
            }
        }
    }
    return count;
}
0

精彩评论

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