【LeetCode】37. Solving Sudoku

1. Title

Write a program to solve the Sudoku problem through the filled spaces.
A Sudoku solution must follow the following rules :

The numbers 1-9 can only appear once per line.
The numbers 1-9 can only appear once in each column.
The numbers 1-9 can only appear once in each 3x3 palace separated by a thick solid line.
Blank cells are represented by'.'.
Sudoku
A Sudoku. The answer is marked in red.

Note:

  • The given Sudoku sequence only contains numbers 1-9 and the character'.'.
  • You can assume that a given Sudoku has only one unique solution.
  • A given Sudoku is always in 9x9 format.

Two, solve

1. Recursion-scenario simulation

version 1

Ideas:

Traverse each blank cell line by line, try to fill in the number x (range: 1-9) in each blank cell, and then check whether the row, column, and 3*3 house are repeated. If it is repeated, return directly and return to the previous cell. Then fill in x+1 and try again. If all the numbers are qualified after the traversal is completed, the two-dimensional array filled with numbers is returned.

Code:

class Solution {
    
    
    public void solveSudoku(char[][] board) {
    
    
        if(board == null || board.length == 0)  return;
        solve(board);
    }
    
    public boolean solve(char[][] board){
    
    
        for(int row = 0; row < board.length; row++){
    
    
            for(int col = 0; col < board[0].length; col++){
    
    
                if(board[row][col] == '.'){
    
    
                    for(char c = '1'; c <= '9'; c++){
    
    //trial. Try 1 through 9
                        if(isValid(board, row, col, c)){
    
    
                            board[row][col] = c; //Put c for this cell
                            if(solve(board))
                                return true; //If it's the solution return true
                            else
                                board[row][col] = '.'; //Otherwise go back
                        }
                    }
                    return false;
                }
            }
        }
        return true;
    }
    
    private boolean isValid(char[][] board, int row, int col, char c){
    
    
        for(int i = 0; i < 9; i++) {
    
    
            if(board[i][col] != '.' && board[i][col] == c) return false; //check row
            if(board[row][i] != '.' && board[row][i] == c) return false; //check column
            if(board[3 * (row / 3) + i / 3][ 3 * (col / 3) + i % 3] != '.' && 
board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == c) return false; //check 3*3 block
        }
        return true;
    }
}

Time complexity: O (9 9 ∗ 9) O(9^{9*9})O ( 99 9 )
Space complexity: O (9 ∗ 9) O(9*9)O ( 99)

Version 2

Ideas:

Basically the same as version 1, except that each grid is numbered from 1 to 81. Then calculate the ranks through the number, and then check the numbers of the rows, columns, and palaces, and backtracking is invalid; valid continue to recurse until the last one.

Code:

class Solution {
    
    
    public void solveSudoku(char[][] board) {
    
    
        if (board==null || board.length<9)  return;
        solveSudokuHelper(0, board);
    }

    public boolean solveSudokuHelper(int index, char[][] board) {
    
    
        int row = index/9, col = index%9;
        if (index==81)  return true;
        else {
    
    
            if (board[row][col]!='.') return solveSudokuHelper(index+1, board);
            else {
    
    
                for (char c='1'; c<='9'; c++) {
    
    
                    if (isValid(board, row, col, c)) {
    
    
                        board[row][col] = c;
                        if (solveSudokuHelper(index+1, board))  return true;
                        else board[row][col] = '.';
                    }
                }
                return false;
            }
        }
    }

    public boolean isValid(char[][] board, int row, int col, char c) {
    
    
        for (int i=0; i<9; i++) {
    
    
            if (board[row][i]!='.' && board[row][i]==c)  return false;
            if (board[i][col]!='.' && board[i][col]==c)  return false;
            if (board[row/3*3+i/3][col/3*3+i%3]!='.' && board[row/3*3+i/3][col/3*3+i%3]==c)  return false;
        }
        return true;
    }
}

Time complexity: O (9 9 ∗ 9) O(9^{9*9})O ( 99 9 )
Space complexity: O (9 ∗ 9) O(9*9)O ( 99)

2. Bit operation

Idea: I do n't quite understand it for the time being.
Code: slightly
time complexity: O (?) O(?)O ( ? )
Space complexity: O (?) O(?)The ( ? )

Three, reference

1、Straight Forward Java Solution Using Backtracking
2、Two very Simple and Neat Java DFS/Backtracking solutions
3、Less than 30 line clean java solution using DFS
4、解数独

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/108585418