LeetCode 37 Sudoku Solver(求解数独)

题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description

Problem : 解决数独问题,给出一个二维数组,将这个数独进行求解。

思路:

  1. 嵌套循环,三层循环体,每一行,每一列,填入从1到9的数字。判断填入之后是否合理
  2. 判断数独是否合理的函数

参考代码:

package leetcode_50;

/***
 *
 * @author pengfei_zheng
 * 求解数独问题
 */
public class Solution37 {
    public static void solveSudoku(char[][] board) {
        if(board==null ||board.length ==0)
                return;
        else
                solve(board);
    }

    private static boolean solve(char[][] board) {
        for(int i = 0 ; i < 9;i++){
            for(int j = 0 ; j < 9;j++){
                if(board[i][j]==‘.‘){
                    for(char c = ‘1‘; c<=‘9‘;c++){
                        if(isValid(board,i,j,c)){
                            board[i][j]=c;
                            if(solve(board))
                                return true;
                            else
                                board[i][j]=‘.‘;
                        }
                    }
                    return false;
                }
            }
        }
        return true;
    }

    private static boolean isValid(char[][] board, int row, int column, char c) {
        for(int i = 0 ; i < 9; i ++){
            if(board[row][i]==c) return false;
            if(board[i][column]==c) return false;
            if(board[3 * (row / 3) + i / 3][ 3 * (column / 3) + i % 3] != ‘.‘ &&
                    board[3 * (row / 3) + i / 3][3 * (column / 3) + i % 3] == c) return false; //check 3*3 block
        }
        return true;
    }
    public static void main(String[]args){
        long start = System.currentTimeMillis();
        char[][] board={{‘8‘,‘.‘,‘.‘,‘.‘,‘.‘,‘.‘,‘.‘,‘.‘,‘.‘},
                        {‘.‘,‘.‘,‘3‘,‘6‘,‘.‘,‘.‘,‘.‘,‘.‘,‘.‘},
                        {‘.‘,‘7‘,‘.‘,‘.‘,‘9‘,‘.‘,‘2‘,‘.‘,‘.‘},
                        {‘.‘,‘5‘,‘.‘,‘.‘,‘.‘,‘7‘,‘.‘,‘.‘,‘.‘},
                        {‘.‘,‘.‘,‘.‘,‘.‘,‘4‘,‘.‘,‘7‘,‘.‘,‘.‘},
                        {‘.‘,‘.‘,‘.‘,‘1‘,‘.‘,‘5‘,‘.‘,‘3‘,‘.‘},
                        {‘.‘,‘.‘,‘1‘,‘.‘,‘.‘,‘.‘,‘.‘,‘6‘,‘8‘},
                        {‘.‘,‘.‘,‘8‘,‘5‘,‘.‘,‘.‘,‘.‘,‘1‘,‘.‘},
                        {‘.‘,‘9‘,‘.‘,‘.‘,‘.‘,‘.‘,‘4‘,‘.‘,‘.‘}
                    };
        solveSudoku(board);
        long end = System.currentTimeMillis();
        for(int i = 0 ; i < 9 ; i ++){
            for(int j = 0 ; j < 9 ;j ++){
                System.out.print(board[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("耗时: "+ (double)(end-start)/1000+" s");
    }
}
时间: 2024-10-13 06:30:13

LeetCode 37 Sudoku Solver(求解数独)的相关文章

[LeetCode] 37. Sudoku Solver 求解数独

Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column.

[LeetCode] Sudoku Solver 求解数独

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 这道求解数独的题是在之

Java [leetcode 37]Sudoku Solver

题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 解题思路:

LeetCode 37 Sudoku Solver (C,C++,Java,Python)

Problem: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. So

[leetcode 37]sudoku solver

1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循环,直到填完为止. 后来发现,这个思路只能解决部分数独.还有部分数独是需要回溯的,比如,这个位置只能填3或5,那么就需要先填上3,看看能否继续填下去,不能的话,再回过来填5. 想了半天,想不出来,把别人的backtracking看懂了,写出来了.. 3 代码: 自己的: Hashtable<Inte

leetcode 37 Sudoku Solver java

求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution { public void solveSudoku(char[][] board) { HashSet[] hashset = new HashSet[27]; for (int i = 0; i < 27; i++) hashset[i] = new HashSet<Character>

LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku boa

【leetcode】 Sudoku Solver

问题: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 说明: 数独有

37. Sudoku Solver(js)

37. Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly on