编写一个程序,通过填充空格来解决数独问题。
一个数独的解法需遵循如下规则:
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。
空白格用 '.' 表示。
一个数独。
答案被标成红色。
提示:
给定的数独序列只包含数字 1-9 和字符 '.' 。
你可以假设给定的数独只有唯一解。
给定数独永远是 9x9 形式的。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sudoku-solver
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
package algorithmtest;
import java.util.HashSet;
import java.util.Set;
public class sudoku_solver {
public void solveSudoku(char[][] board) {
// 存储最终的结果
char[][] boardResult = new char[board.length][board[0].length];
dfs(board, boardResult, 0);
arrClone(boardResult, board);
}
/**
* @param board
* @param start 填到哪个数字了
*/
public void dfs(char[][] board, char[][] boardResult, int start) {
// 填满之后,输出结果
if (start == board.length * board.length) {
arrClone(board, boardResult);
return;
}
int curRow = start / board.length;
int curCol = start % board.length;
// 已经填过数字的,就直接进入下一个
if (board[curRow][curCol] != '.') {
dfs(board, boardResult, start + 1);
} else {
for (char c = '1'; c <= '9'; c++) {
// 检测start处是否能填入c字符
if (isCanInput(board, start, c)) {
board[curRow][curCol] = c;
dfs(board, boardResult, start + 1);
board[curRow][curCol] = '.';
}
}
}
}
/**
* @param board
* @param start 填到第几个数字
* @param c 填的数字
* @return
*/
public boolean isCanInput(char[][] board, int start, char c) {
// 从第几行的数组中找
int curRow = start / board.length;
// 第几个元素
int curCol = start % board.length;
// 宫检测
int beginRow = curRow / 3 * 3;
int beginCol = curCol / 3 * 3;
int n = beginRow * board.length + beginCol;
int[] indexList = {
n, n + 1, n + 2,
n + 9, n + 10, n + 11,
n + 18, n + 19, n + 20,
};
Set<Character> s = new HashSet<>();
int num = 0;
for (int i : indexList) {
int curR = i / board.length;
int curC = i % board.length;
if (board[curR][curC] != '.') {
s.add(board[curR][curC]);
num++;
}
}
if (s.size() != num) {
return false;
}
// 横向检测
for (int startY = 0; startY < board.length; startY++) {
if (startY == curCol) {
continue;
}
if (board[curRow][startY] == c) {
return false;
}
}
// 竖向检测
for (int startX = 0; startX < board.length; startX++) {
if (startX == curRow) {
continue;
}
if (board[startX][curCol] == c) {
return false;
}
}
return true;
}
private void printMap(char[][] board) {
for (int x = 0; x < board.length; x++) {
for (int y = 0; y < board.length; y++) {
System.out.printf("%2c", board[x][y]);
}
System.out.println();
}
System.out.println("");
}
private void arrClone(char[][] src, char[][] dst) {
for (int i = 0; i < src.length; i++) {
for (int j = 0; j < src[0].length; j++) {
dst[i][j] = src[i][j];
}
}
}
public static void main(String[] args) {
char[][] 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'}
};
sudoku_solver ss = new sudoku_solver();
ss.solveSudoku(board);
ss.printMap(board);
}
}
/*
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
*/
注意:
char[][] arr = src.clone(); 这样当src改变的时候,arr也会跟着改变,也就是这里的clone并不是深拷贝。 因此必须写一个arrClone方法进行立面元素的一个个赋值。