-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathSudoku
66 lines (62 loc) · 1.62 KB
/
Sudoku
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
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' } };
solve(0, 0, board);
}
public static void solve(int r, int c, char[][] board) {
if (c == 9) {
c = 0;
r++;
}
if (r == 9) {
print(board);
System.out.println("===============");
return;
}
if (board[r][c] != '.') {
solve(r, c + 1, board);
} else {
for (int i = 1; i <= board.length; i++) {
if (isSafe(r, c, i, board)) {
board[r][c] = (char) ('0' + i);
solve(r, c + 1, board);
}
board[r][c] = '.';
}
}
}
public static boolean isSafe(int r, int c, int i, char[][] board) {
char ch = (char) ('0' + i);
for (int R = 0; R < 9; R++) {
if (board[R][c] == ch) {
return false;
}
}
for (int C = 0; C < 9; C++) {
if (board[r][C] == ch) {
return false;
}
}
int box_r = r / 3;
int box_c = c / 3;
for (int R = box_r * 3; R < box_r * 3 + 3; R++) {
for (int C = box_c * 3; C < box_c * 3 + 3; C++) {
if (board[R][C] == ch) {
return false;
}
}
}
return true;
}
public static void print(char[][] board) {
for (char[] row : board) {
for (char b : row) {
System.out.print(b + " ");
}
System.out.println();
}
}
}