题目
编写一个程序,通过填充空格来解决数独问题。
数独的解法需 遵循如下规则:
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
数独部分空格内已填入了数字,空白格用 ‘.’ 表示。
示例:
输入: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”]]
解释:输入的数独如上图所示,唯一有效的解决方案如下所示:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sudoku-solver
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解答
package Hash01;
public class example04 {
public void solveSudoku(char[][] board) {
// 先输入初始情况
// 数独分三种情况初始化,方便以后的判断
boolean[][] row = new boolean[9][9]; // 行
boolean[][] col = new boolean[9][9]; // 列
boolean[][] block = new boolean[9][9]; // 块
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int num = board[i][j] - '1';// 这里是字符的减法
row[i][num] = true;
col[j][num] = true;
block[i / 3 * 3 + j / 3][num] = true;
}
}
}
dfs(board, row, col, block, 0, 0);
}
// 这里采用深度优先算法来解决
// 这里的返回值是boolean,其实就是一个钩子,这是起到一个判断是否合适的作用
private boolean dfs(char[][] board, boolean[][] row, boolean[][] col, boolean[][] block, int i, int j) {
// 结束条件
while (board[i][j] != '.') {
// 9个一轮回
if (++j >= 9) {
++i;
j = 0;
}
// 说明全部搜索完成了
if (i >= 9) {
return true;
}
}
// 下面是算法的精髓之处,利用回溯来解决
for (int num = 0; num < 9; num++) {
int blockIndex = i / 3 * 3 + j / 3; // 定位所在块的位置
if (!row[i][num] && !col[j][num] && !block[blockIndex][num]) {
board[i][j] = (char) ('1' + num); // 注意,前面如果没有加char那么是有问题的就是‘1’转化为ASCII码和num相加了
row[i][num] = true;
col[j][num] = true;
block[blockIndex][num] = true;
if (dfs(board, row, col, block, i, j)) {
return true;
} else {
// 这里失败了就需要进行回溯
board[i][j] = '.';
row[i][num] = false;
col[j][num] = false;
block[blockIndex][num] = false;
}
}
}
return false;
}
//打印出最后的结果
public void print(char[][] board) {
for(int i = 0; i < 9; i ++) {
for(int j = 0; j < 9; j ++) {
System.out.print(board[i][j] + "\t");
}
System.out.println();
}
}
}
Main测试
public class Test06 {
public static void main(String[] args) {
String[][] 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"}
};
char[][] newBoard = new char[board.length][board[0].length];
for(int i = 0; i < board.length; i ++) {
for(int j = 0; j < board[0].length; j ++) {
newBoard[i][j] = board[i][j].charAt(0);
}
}
example04 a = new example04();
System.out.println("原来的数独为");
a.print(newBoard);
a.solveSudoku(newBoard);
System.out.println("现在的数独为");
a.print(newBoard);
}
}
执行结果
原来的数独为
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
总结
本题采用深度优先+回溯的方法解决,思路和八皇后问题相似,注意数独有三条规则,所以这里需要三个数组进行存储判断,其中块数组的判断采用i / 3 * 3 + j / 3的方式,来索引,非常的巧妙,本题的答案采录于网友回答,加上笔者自己的一些思考所得。