-
Notifications
You must be signed in to change notification settings - Fork 14
/
37-moran991231.java
58 lines (51 loc) · 1.29 KB
/
37-moran991231.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.ArrayList;
class Solution {
boolean[][] exist_row;
boolean[][] exist_col;
boolean[][] exist_sqr;
char[][] board;
ArrayList<int[]> blanks;
public boolean dfs(int depth) {
if (depth < 0) {
return true;
}
int[] ij = blanks.get(depth);
int i = ij[0], j = ij[1], sqr;
sqr = i / 3 * 3 + j / 3;
for (int n = 1; n <= 9; n++) {
if (!(exist_row[i][n] || exist_col[j][n] || exist_sqr[sqr][n])) {
exist_row[i][n] = exist_col[j][n] = exist_sqr[sqr][n] = true;
board[i][j] = (char) n;
if (dfs(depth - 1))
return true;
exist_row[i][n] = exist_col[j][n] = exist_sqr[sqr][n] = false;
board[i][j] = 0;
}
}
return false;
}
public void solveSudoku(char[][] board) {
exist_row = new boolean[9][10];
exist_col = new boolean[9][10];
exist_sqr = new boolean[9][10];
this.board = board;
blanks = new ArrayList<>();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
board[i][j] = 0;
blanks.add(new int[] { i, j });
} else
board[i][j] -= '0';
char n = board[i][j];
exist_row[i][n] = true;
exist_col[j][n] = true;
exist_sqr[i / 3 * 3 + j / 3][n] = true;
}
}
dfs(blanks.size() - 1);
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
board[i][j] += '0';
}
}