Skip to content

Commit 02272d9

Browse files
committed
Added task 37.
1 parent 819c638 commit 02272d9

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package s0037_sudoku_solve;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Solution {
7+
List<int[]> emptyCells = new ArrayList<>();
8+
int[] rows = new int[9];
9+
int[] cols = new int[9];
10+
int[] boxes = new int[9];
11+
12+
public void solveSudoku(char[][] board) {
13+
for (int r = 0; r < 9; r++) {
14+
for (int c = 0; c < 9; c++) {
15+
if (board[r][c] == '.') {
16+
emptyCells.add(new int[] {r, c});
17+
} else {
18+
int val = board[r][c] - '0';
19+
int boxPos = r / 3 * 3 + c / 3;
20+
rows[r] |= 1 << val;
21+
cols[c] |= 1 << val;
22+
boxes[boxPos] |= 1 << val;
23+
}
24+
}
25+
}
26+
backtracking(board, 0);
27+
}
28+
29+
boolean backtracking(char[][] board, int i) {
30+
// Check if we filled all empty cells?
31+
if (i == emptyCells.size()) {
32+
return true;
33+
}
34+
35+
int r = emptyCells.get(i)[0];
36+
int c = emptyCells.get(i)[1];
37+
int boxPos = (r / 3) * 3 + c / 3;
38+
for (int val = 1; val <= 9; ++val) {
39+
// skip if that value is existed!
40+
if (hasBit(rows[r], val) || hasBit(cols[c], val) || hasBit(boxes[boxPos], val)) {
41+
continue;
42+
}
43+
board[r][c] = (char) ('0' + val);
44+
// backup old values
45+
int oldRow = rows[r];
46+
int oldCol = cols[c];
47+
int oldBox = boxes[boxPos];
48+
rows[r] |= 1 << val;
49+
cols[c] |= 1 << val;
50+
boxes[boxPos] |= 1 << val;
51+
if (backtracking(board, i + 1)) {
52+
return true;
53+
}
54+
rows[r] = oldRow;
55+
cols[c] = oldCol;
56+
boxes[boxPos] = oldBox;
57+
}
58+
return false;
59+
}
60+
61+
boolean hasBit(int x, int k) {
62+
return (x >> k & 1) == 1;
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package s0037_sudoku_solve;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void solveSudoku() {
11+
char[][] expected = {
12+
{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
13+
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
14+
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
15+
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
16+
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
17+
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
18+
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
19+
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
20+
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}
21+
};
22+
char[][] board = {
23+
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
24+
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
25+
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
26+
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
27+
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
28+
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
29+
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
30+
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
31+
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}
32+
};
33+
new Solution().solveSudoku(board);
34+
assertThat(board, equalTo(expected));
35+
}
36+
}

0 commit comments

Comments
 (0)