/
Solution.java
executable file
·175 lines (137 loc) · 5.07 KB
/
Solution.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.util.*;
public class Solution {
Boolean[][] rows = new Boolean[9][9], col = new Boolean[9][9];
Boolean[][][] x = new Boolean[3][3][9];
public Boolean helper(char[][] board, int idx, int idy) {
if (idy > 8) return helper(board, idx + 1, 0);
if (idx > 8) return true;
if (board[idx][idy] == '.') {
for (int i = 0; i < 9; i++) {
if (!rows[idx][i] &&
!col[idy][i] &&
!x[idx / 3][idy / 3][i]) {
rows[idx][i] = true;
col[idy][i] = true;
x[idx / 3][idy / 3][i] = true;
board[idx][idy] = (char) (i + (int) '1');
if (helper(board, idx, idy + 1)) {
return true;
}
board[idx][idy] = '.';
rows[idx][i] = false;
col[idy][i] = false;
x[idx / 3][idy / 3][i] = false;
}
}
return false;
} else {
return helper(board, idx, idy + 1);
}
}
public void solveSudoku(char[][] board) {
for(int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
for (int k = 0; k < 9; k++) {
rows[i][k] = false;
col[j][k] = false;
x[i / 3][j / 3][k] = false;
}
}
}
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
if(board[i][j] != '.'){
int tmp = (int)board[i][j] - (int)'1';
rows[i][tmp] = true;
col[j][tmp] = true;
x[i/3][j/3][tmp] = true;
}
}
}
helper(board, 0, 0);
}
}
public class Solution {
static final List<Character> VALID = Arrays.asList(new Character[]{ '1' , '2', '3', '4', '5', '6', '7', '8', '9'});
boolean _solveSudoku(char[][] board){
int mx = board.length;
int my = board[0].length;
int x ,y;
for(x = 0; x < mx; x++){
for(y = 0; y < my; y++){
if(board[x][y] == '.'){
int _x, _y;
HashSet<Character> val = new HashSet<Character>(VALID);
for(_y = 0; _y < my; _y++)
val.remove(board[x][_y]);
for(_x = 0; _x < mx; _x++)
val.remove(board[_x][y]);
int sx = x / 3 * 3;
int sy = y / 3 * 3;
for(int offset = 0; offset < 9; offset++){
int ox = offset % 3;
int oy = offset / 3;
val.remove(board[sx + ox][sy + oy]);
}
for(char c : val){
board[x][y] = c;
if(isValidSudoku(board)){
// try next '.'
if(_solveSudoku(board)){
return true;
}
// try another number
}
board[x][y] = '.';
}
// board[x][y] is '.' return false to fail fast
return false;
}
}
}
return true;
}
public void solveSudoku(char[][] board) {
_solveSudoku(board);
}
boolean isValidSudoku(char[][] board) {
int mx = board.length;
int my = board[0].length;
int x, y;
for(x = 0; x < mx; x++){
HashSet<Character> col = new HashSet<Character>();
for(y = 0; y < my; y++){
char c = board[x][y];
if(c != '.'){
if(col.contains(c)) return false;
col.add(c);
}
}
}
for(y = 0; y < my; y++){
HashSet<Character> row = new HashSet<Character>();
for(x = 0; x < mx; x++){
char c = board[x][y];
if(c != '.'){
if(row.contains(c)) return false;
row.add(c);
}
}
}
for(x = 0; x < mx; x += 3){
for(y = 0; y < my; y += 3){
HashSet<Character> block = new HashSet<Character>();
for(int offset = 0; offset < 9; offset++){
int ox = offset % 3;
int oy = offset / 3;
char c = board[x + ox][y + oy];
if(c != '.'){
if(block.contains(c)) return false;
block.add(c);
}
}
}
}
return true;
}
}