-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuSolver.java
More file actions
105 lines (94 loc) · 3.64 KB
/
SudokuSolver.java
File metadata and controls
105 lines (94 loc) · 3.64 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package hard;
/**
* ClassName: SudokuSolver
* @author chenyiAlone
* Create Time: 2019/03/19 16:55:33
* Description: No.37
* 思路:
* 深度优先搜索 + 回溯
*
* 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 9 3x3 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.
*/
public class SudokuSolver {
private boolean found = false;
public void solveSudoku(char[][] board) {
if (board == null || board.length == 0) return;
DFSfileBoard(board, 0, 0);
}
public void DFSfileBoard(char[][] board, int l, int c) {
if (c == 9 && l == 8) {
found = true;
util.Utils.printCharArray(board);
return;
}
if (found) return;
if (c == 9) {
c = 0;
l += 1;
}
if (board[l][c] == '.') {
for (char i = '1'; i <= '9'; i++) {
if (check(l, c, board, i)) {
board[l][c] = i;
DFSfileBoard(board, l, c + 1);
}
// 回溯法的精髓,重置原先的值才能实现退步回溯
board[l][c] = '.';
}
} else {
DFSfileBoard(board, l, c + 1);
}
}
public boolean check(int l, int c, char[][] board, char n) {
for (int i = 0; i < 9; i++)
if (board[i][c] == n || board[l][i] == n)
return false;
int tl = (l / 3) * 3;
int tc = (c / 3) * 3;
// System.out.println(tl + " " + tc);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[tl + i][tc + j] == n)
return false;
}
}
return true;
}
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'}
};
char[][] board2 = {
{'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'}
};
new SudokuSolver().solveSudoku(board);
// System.out.println(new SudokuSolver().check(0, 0, board2, '5'));
}
}