[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.
Each of the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.
The ‘.’ character indicates empty cells.

Example 1:
Example 1
Input: board = [[“5”,”3”,”.”,”.”,”7”,”.”,”.”,”.”,”.”],[“6”,”.”,”.”,”1”,”9”,”5”,”.”,”.”,”.”],[“.”,”9”,”8”,”.”,”.”,”.”,”.”,”6”,”.”],[“8”,”.”,”.”,”.”,”6”,”.”,”.”,”.”,”3”],[“4”,”.”,”.”,”8”,”.”,”3”,”.”,”.”,”1”],[“7”,”.”,”.”,”.”,”2”,”.”,”.”,”.”,”6”],[“.”,”6”,”.”,”.”,”.”,”.”,”2”,”8”,”.”],[“.”,”.”,”.”,”4”,”1”,”9”,”.”,”.”,”5”],[“.”,”.”,”.”,”.”,”8”,”.”,”.”,”7”,”9”]]

Output: [[“5”,”3”,”4”,”6”,”7”,”8”,”9”,”1”,”2”],[“6”,”7”,”2”,”1”,”9”,”5”,”3”,”4”,”8”],[“1”,”9”,”8”,”3”,”4”,”2”,”5”,”6”,”7”],[“8”,”5”,”9”,”7”,”6”,”1”,”4”,”2”,”3”],[“4”,”2”,”6”,”8”,”5”,”3”,”7”,”9”,”1”],[“7”,”1”,”3”,”9”,”2”,”4”,”8”,”5”,”6”],[“9”,”6”,”1”,”5”,”3”,”7”,”2”,”8”,”4”],[“2”,”8”,”7”,”4”,”1”,”9”,”6”,”3”,”5”],[“3”,”4”,”5”,”2”,”8”,”6”,”1”,”7”,”9”]]
Explanation: The input board is shown above and the only valid solution is shown below:
Example

Constraints:
board.length == 9
board[i].length == 9
board[i][j] is a digit or ‘.’.
It is guaranteed that the input board has only one solution.

解数独。

编写一个程序,通过填充空格来解决数独问题。

数独的解法需 遵循如下规则:

数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
数独部分空格内已填入了数字,空白格用 ‘.’ 表示。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/sudoku-solver
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

需要先做 36 题。做完 36 题,37 题需要做的就是根据规则填充 input。填充的规则是如果当前坐标上是一个点(.)的话,尝试放 1 - 9 之间的一个数字,放进去之后判断两件事,1 是是否有效(这个数字在当前行/列/九宫格是否被放过了),2 是放进去之后接着往下迭代处理剩下的部分的时候看看是否依然能解决完整个数独。所以这道题会牵涉到 DFS 回溯。

复杂度

时间 O(9^(n*n)),其中 n 是数独的边长
空间 O(1)

代码

Java实现一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public void solveSudoku(char[][] board) {
if (board == null || board.length == 0) {
return;
}
helper(board);
}

private boolean helper(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 (helper(board)) {
return true;
} else {
board[i][j] = '.';
}
}
}
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;
if (board[row][i] != '.' && board[row][i] == 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;
}
}

Java实现二

我这里再提供一种更好理解的做法。首先我们还是创建三个 boolean 的二维数组记录已经存在的数字,然后我们需要一个 helper 函数去做 backtracking,模拟完其他还不存在的数字。helper 函数的退出条件是模拟到了最右下角的坐标;在没有模拟完毕之前,对于每个还没有填充过数字的坐标,我们都尝试去填充从 1 到 9 的每一个数字。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
public void solveSudoku(char[][] board) {
// 三个布尔数组 表明 行, 列, 还有 3*3 的方格的数字是否被使用过
boolean[][] rowUsed = new boolean[9][10];
boolean[][] colUsed = new boolean[9][10];
boolean[][][] boxUsed = new boolean[3][3][10];
// 初始化
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[0].length; col++) {
int num = board[row][col] - '0';
if (1 <= num && num <= 9) {
rowUsed[row][num] = true;
colUsed[col][num] = true;
boxUsed[row / 3][col / 3][num] = true;
}
}
}
// 递归尝试填充数组
helper(board, rowUsed, colUsed, boxUsed, 0, 0);
}

private boolean helper(char[][] board, boolean[][] rowUsed, boolean[][] colUsed, boolean[][][] boxUsed, int row,
int col) {
// 边界校验, 如果已经填充完成, 返回true, 表示一切结束
if (col == board[0].length) {
col = 0;
row++;
if (row == board.length) {
return true;
}
}
// 是空则尝试填充, 否则跳过继续尝试填充下一个位置
if (board[row][col] == '.') {
// 尝试填充1~9
for (int num = 1; num <= 9; num++) {
boolean canUsed = !(rowUsed[row][num] || colUsed[col][num] || boxUsed[row / 3][col / 3][num]);
if (canUsed) {
rowUsed[row][num] = true;
colUsed[col][num] = true;
boxUsed[row / 3][col / 3][num] = true;
board[row][col] = (char) ('0' + num);
if (helper(board, rowUsed, colUsed, boxUsed, row, col + 1)) {
return true;
}
board[row][col] = '.';
rowUsed[row][num] = false;
colUsed[col][num] = false;
boxUsed[row / 3][col / 3][num] = false;
}
}
} else {
return helper(board, rowUsed, colUsed, boxUsed, row, col + 1);
}
return false;
}
}

相关题目

1
2
3
36. Valid Sudoku
37. Sudoku Solver
2133. Check if Every Row and Column Contains All Numbers

[LeetCode] 37. Sudoku Solver
https://shurui91.github.io/posts/1704254952.html
Author
Aaron Liu
Posted on
April 20, 2020
Licensed under