友情支持
如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜
有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。
公众号的微信号是: jikerizhi 。因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。 |
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 the digits
1-9
must occur exactly once in each of the 93x3
sub-boxes of the grid.
Empty cells are indicated by the character '.'
.
A sudoku puzzle…
…and its solution numbers marked in red.
Note:
-
The given board contain only digits
1-9
and the character'.'
. -
You may assume that the given Sudoku puzzle will have a single unique solution.
-
The given board size is always
9x9
.
解题分析
这道题特别需要关注的点是:只要找到一个解就可以了,不需要求全部解。所以,需要重点思考的是,如何在找到第一个解时,就立即停止搜索。
递归树
-
一刷
-
二刷
-
二刷(引入步进)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* * @author D瓜哥 · https://www.diguage.com
* * @since 2020-03-25 09:34
*/
public void solveSudoku(char[][] board) {
backtrack(board, 0);
}
/**
* Runtime: 17 ms, faster than 35.23% of Java online submissions for Sudoku Solver.
* Memory Usage: 37 MB, less than 21.05% of Java online submissions for Sudoku Solver.
*/
private boolean backtrack(char[][] board, int step) {
int len = board.length;
if (step == len * len) {
return true;
}
int y = step / len;
int x = step % len;
for (int i = y; i < len; i++) {
for (int j = x; j < len; j++) {
if (board[i][j] != '.') {
return backtrack(board, step + 1);
}
for (char c = '1'; c <= '9'; c++) {
if (!isValid(board, i, j, c)) {
continue;
}
board[i][j] = c;
System.out.printf("y=%d,x=%d, num=%s, step=%d %n",
y, x, c, step);
Printers.printMatrix(board);
if (backtrack(board, step + 1)) {
return true;
}
board[i][j] = '.';
}
return false;
}
}
return false;
}
private boolean isValid(char[][] board, int y, int x, char c) {
for (int i = 0; i < 9; i++) {
if (board[(y / 3) * 3 + i / 3][(x / 3) * 3 + i % 3] == c
|| board[y][i] == c
|| board[i][x] == c) {
return false;
}
}
return true;
}
/**
* Copy From
*/
private boolean backtrackXY(char[][] board, int y, int x) {
int len = board.length;
if (x == len) {
return backtrackXY(board, y + 1, 0);
}
if (y == len) {
return true;
}
for (int i = y; i < len; i++) {
for (int j = x; j < len; j++) {
if (board[i][j] != '.') {
return backtrackXY(board, i, j + 1);
}
for (char c = '1'; c <= '9'; c++) {
if (!isValid(board, i, j, c)) {
continue;
}
board[i][j] = c;
System.out.printf("y=%d,x=%d, num=%s %n", i, j, c);
Printers.printMatrix(board);
if (backtrackXY(board, i, j + 1)) {
return true;
}
board[i][j] = '.';
}
return false;
}
}
return false;
}
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* * @author D瓜哥 · https://www.diguage.com
* * @since 2024-09-11 10:59:08
*/
public void solveSudoku(char[][] board) {
backtracking(board);
}
private boolean backtracking(char[][] board) {
for (int r = 0; r < board.length; r++) {
for (int c = 0; c < board[r].length; c++) {
if (board[r][c] != '.') {
continue;
}
// 根据限制条件,筛选出可选字符,否则重复判断
char[] chars = selectChars(board, r, c);
for (char sc : chars) {
board[r][c] = sc;
printMatrix(board);
if (backtracking(board)) {
return true;
}
board[r][c] = '.';
}
return false;
}
}
return true;
}
private char[] selectChars(char[][] board, int row, int column) {
char[] chars = new char[9];
int length = 9;
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) ('1' + i);
}
for (int i = 0; i < board.length; i++) {
char c = board[i][column];
if (c != '.' && chars[c - '1'] != '.') {
chars[c - '1'] = '.';
length--;
if (length == 0) {
return new char[0];
}
}
}
for (int i = 0; i < board[row].length; i++) {
char c = board[row][i];
if (c != '.' && chars[c - '1'] != '.') {
chars[c - '1'] = '.';
length--;
if (length == 0) {
return new char[0];
}
}
}
row = (row / 3) * 3;
column = (column / 3) * 3;
for (int i = row; i < row + 3; i++) {
for (int j = column; j < column + 3; j++) {
char c = board[i][j];
if (c != '.' && chars[c - '1'] != '.') {
chars[c - '1'] = '.';
length--;
if (length == 0) {
return new char[0];
}
}
}
}
char[] result = new char[length];
for (int i = chars.length - 1; i >= 0; i--) {
if (chars[i] != '.') {
result[--length] = chars[i];
}
}
return result;
}
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* * @author D瓜哥 · https://www.diguage.com
* * @since 2024-09-11 10:59:08
*/
public void solveSudoku(char[][] board) {
backtracking(board, 0);
}
private boolean backtracking(char[][] board, int step) {
int row = step / board.length;
int column = step % board.length;
for (int r = row; r < board.length; r++) {
for (int c = column; c < board[r].length; c++) {
if (board[r][c] != '.') {
return backtracking(board, step + 1);
}
// 根据限制条件,筛选出可选字符,否则重复判断
char[] chars = selectChars(board, r, c);
for (char sc : chars) {
board[r][c] = sc;
printMatrix(board);
if (backtracking(board, step + 1)) {
return true;
}
board[r][c] = '.';
}
return false;
}
}
return true;
}
private char[] selectChars(char[][] board, int row, int column) {
char[] chars = new char[9];
int length = 9;
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) ('1' + i);
}
for (int i = 0; i < board.length; i++) {
char c = board[i][column];
if (c != '.' && chars[c - '1'] != '.') {
chars[c - '1'] = '.';
length--;
if (length == 0) {
return new char[0];
}
}
}
for (int i = 0; i < board[row].length; i++) {
char c = board[row][i];
if (c != '.' && chars[c - '1'] != '.') {
chars[c - '1'] = '.';
length--;
if (length == 0) {
return new char[0];
}
}
}
row = (row / 3) * 3;
column = (column / 3) * 3;
for (int i = row; i < row + 3; i++) {
for (int j = column; j < column + 3; j++) {
char c = board[i][j];
if (c != '.' && chars[c - '1'] != '.') {
chars[c - '1'] = '.';
length--;
if (length == 0) {
return new char[0];
}
}
}
}
char[] result = new char[length];
for (int i = chars.length - 1; i >= 0; i--) {
if (chars[i] != '.') {
result[--length] = chars[i];
}
}
return result;
}