题目
编写一个程序,通过填充空格来解决数独问题。
数独的解法需 遵循如下规则:
- 数字
1-9
在每一行只能出现一次。 - 数字
1-9
在每一列只能出现一次。 - 数字
1-9
在每一个以粗实线分隔的3x3
宫内只能出现一次。(请参考示例图)
数独部分空格内已填入了数字,空白格用 '.'
表示。
示例 1:
输入: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]
是一位数字或者'.'
- 题目数据 保证 输入数独仅有一个解
解答
我们可以考虑按照「行优先」的顺序依次枚举每一个空白格中填的数字,通过递归 + 回溯的方法枚举所有可能的填法。当递归到最后一个空白格后,如果仍然没有冲突,说明我们找到了答案;在递归的过程中,如果当前的空白格不能填下任何一个数字,那么就进行回溯。
由于每个数字在同一行、同一列、同一个九宫格中只会出现一次,因此我们可以使用line[i],column[j],block[x][y]
分别表示第 i 行,第 j 列,第(x,y) 个九宫格中填写数字的情况。在下面给出的三种方法中,我们将会介绍两种不同的表示填写数字情况的方法。
九宫格的范围为 $0 \leq x \leq 2$以及 $0 \leq y \leq 2$。 具体地,第 i 行第 j 列的格子位于第$(\lfloor i/3 \rfloor, \lfloor j/3 \rfloor)$个九宫格中,其中$\lfloor u \rfloor$表示对 u 向下取整。
- 回溯法
思路:最容易想到的方法是用一个数组记录每个数字是否出现。由于我们可以填写的数字范围为[1,9],而数组的下标从 0 开始,因此在存储时,我们使用一个长度为 9 的布尔类型的数组,其中 i 个元素的值为 $\text{True}$,当且仅当数字i+1 出现过。例如我们用 $\textit{line}[2][3] = \text{True}$ 表示数字 4 在第 2 行已经出现过,那么当我们在遍历到第 2 行的空白格时,就不能填入数字 4。
算法:
首先对整个数独数组进行遍历,当我们遍历到第i行第j列时:
- 若该位置是一个空白格,那么我们将其加入一个用来存储空白格位置的列表中,方便后续的递归操作;
- 若该位置是一个数字x,那么我们需要将
line[i][x-1],column[j][x-1]
以及block[i/3][j/3][x-1]
都置为True。
当我们结束了遍历过程之后,就可以开始递归枚举。当递归到第 $i$ 行第 $j$ 列的位置时,我们枚举填入的数字 $x$。根据题目的要求,数字 $x$ 不能和当前行、列、九宫格中已经填入的数字相同,因此 $line[i][x-1]$,$\textit{column}[j][x-1]$以及 $\textit{block}[\lfloor i/3 \rfloor][\lfloor j/3 \rfloor][x-1]$ 必须均为 $\text{False}$。
当我们填入了数字 $x$ 之后,我们要将上述的三个值都置为 $\text{True}$,并且继续对下一个空白格位置进行递归。在回溯到当前递归层时,我们还要将上述的三个值重新置为 $\text{False}$。
class Solution {
private:
bool line[9][9];
bool column[9][9];
bool block[3][3][9];
bool valid;
vector<pair<int, int>> spaces;
public:
void dfs(vector<vector<char>>& board, int pos) {
if (pos == spaces.size()) {
valid = true;
return;
}
auto [i, j] = spaces[pos]; //获取空位位置
for (int digit = 0; digit < 9 && !valid; ++digit) {
if (!line[i][digit] && !column[j][digit] && !block[i / 3][j / 3][digit]) {
line[i][digit] = column[j][digit] = block[i / 3][j / 3][digit] = true; //填符合条件的数字
board[i][j] = digit + '0' + 1;
dfs(board, pos + 1); //递归
line[i][digit] = column[j][digit] = block[i / 3][j / 3][digit] = false; //回溯
}
}
}
void solveSudoku(vector<vector<char>>& board) {
memset(line, false, sizeof(line));
memset(column, false, sizeof(column));
memset(block, false, sizeof(block));
valid = false;
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
if (board[i][j] == '.') {
spaces.emplace_back(i, j); //initialize
}
else {
int digit = board[i][j] - '0' - 1;
line[i][digit] = column[j][digit] = block[i / 3][j / 3][digit] = true; //initialize
}
}
}
dfs(board, 0); //从头开始求解,最后得到填满数字的board
}
};
参考
力扣官方题解:https://leetcode.cn/problems/sudoku-solver/solutions/414120/jie-shu-du-by-leetcode-solution/