-
Notifications
You must be signed in to change notification settings - Fork 0
/
SudokuSolver.java
44 lines (42 loc) · 1.48 KB
/
SudokuSolver.java
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
public class Solution {
public void solveSudoku(char[][] board) {
solveSudokuHelper(board);
}
private boolean solveSudokuHelper(char[][] board) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
char[] possible = collectPossible(board, i, j);
for (int k = 1; k <= 9; k++) {
if (possible[k] == 1)
continue;
board[i][j] = (char)('0' + k);
if (solveSudokuHelper(board))
return true;
}
board[i][j] = '.';
return false;
}
}
}
return true;
}
private char[] collectPossible(char[][] board, int row, int col) {
char[] ret = new char[10];
for (int i = 0; i < 9; i++)
if (board[i][col] != '.' && i != row)
ret[board[i][col] - '0'] = 1;
for (int j = 0; j < 9; j++)
if (board[row][j] != '.' && j != col)
ret[board[row][j] - '0'] = 1;
for (int i = row / 3 * 3, ie = i + 3; i < ie; i++) {
for (int j = col / 3 * 3, je = j + 3; j < je; j++) {
if (i == row && j == col)
continue;
if (board[i][j] != '.')
ret[board[i][j] - '0'] = 1;
}
}
return ret;
}
}