LC 37. 解数独

题目描述

这是 LeetCode 上的 37. 解数独 ,难度为 困难

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

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

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 1-9 在每一个以粗实线分隔的 3 x 3 宫内只能出现一次。(请参考示例图)

数独部分空格内已填入了数字,空白格用 ‘.’ 表示。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
输入: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"]]

输出:[["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"]]

解释:输入的数独如上图所示,唯一有效的解决方案如下所示:

提示:

  • $board.length == 9$
  • $board[i].length == 9$
  • board[i][j] 是一位数字或者 '.'
  • 题目数据 保证 输入数独仅有一个解

回溯解法

和 N 皇后一样,是一道回溯解法裸题。

上一题「36. 有效的数独(中等)」是让我们判断给定的 borad 是否为有效数独。

这题让我们对给定 board 求数独,由于 board 固定是 9 * 9 的大小,我们可以使用回溯算法去做。

这一类题和 N 皇后一样,属于经典的回溯算法裸题。

这类题都有一个明显的特征,就是数据范围不会很大,如该题限制了范围为 9 * 9,而 N 皇后的 N 一般不会超过 13。

对每一个需要填入数字的位置进行填入,如果发现填入某个数会导致数独解不下去,则进行回溯。

代码:

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
class Solution {
boolean[][] row = new boolean[9][9];
boolean[][] col = new boolean[9][9];
boolean[][][] cell = new boolean[3][3][9];
public void solveSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int t = board[i][j] - '1';
row[i][t] = col[j][t] = cell[i / 3][j / 3][t] = true;
}
}
}
dfs(board, 0, 0);
}
boolean dfs(char[][] board, int x, int y) {
if (y == 9) return dfs(board, x + 1, 0);
if (x == 9) return true;
if (board[x][y] != '.') return dfs(board, x, y + 1);
for (int i = 0; i < 9; i++) {
if (!row[x][i] && !col[y][i] && !cell[x / 3][y / 3][i]) {
board[x][y] = (char)(i + '1');
row[x][i] = col[y][i] = cell[x / 3][y / 3][i] = true;
if (dfs(board, x, y + 1)) {
break;
} else {
board[x][y] = '.';
row[x][i] = col[y][i] = cell[x / 3][y / 3][i] = false;
}
}
}
return board[x][y] != '.';
}
}

  • 时间复杂度:在固定 9*9 的棋盘里,具有一个枚举方案的最大值(极端情况,假设我们的棋盘刚开始是空的,这时候每一个格子都要枚举,每个格子都有可能从 $1$ 枚举到 $9$,所以枚举次数为 $9 \times 9 \times 9 = 729$),即复杂度不随数据变化而变化。复杂度为 $O(1)$
  • 空间复杂度:在固定 9*9 的棋盘里,复杂度不随数据变化而变化。复杂度为 $O(1)$

最后

这是我们「刷穿 LeetCode」系列文章的第 No.37 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!