-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp096.java
More file actions
74 lines (62 loc) · 2.26 KB
/
p096.java
File metadata and controls
74 lines (62 loc) · 2.26 KB
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
package level04;
import java.util.List;
import org.junit.Test;
import lib.EulerTest;
public class p096 extends EulerTest {
final int N = 9;
final int K = isqrt(N);
/**
* Find the sum of the first three digits of each of the specified unsolved Sudokus, when
* solved.
*/
@Test
public void test() {
List<String> lines = read();
for (int i = 0; i < lines.size(); i += N + 1) {
int[][] sudoku = new int[N][N];
UsedNums usedNums = new UsedNums();
for (int row : range(N))
for (int col : range(N)) {
int val = lines.get(i + 1 + row).charAt(col) - '0';
sudoku[row][col] = val;
usedNums.update(row, col, val, true);
}
helper(sudoku, 0, 0, usedNums);
}
check(24702);
}
void helper(int[][] sudoku, int row, int col, UsedNums usedNums) {
if (row == N)
ans += Integer.parseInt("" + sudoku[0][0] + sudoku[0][1] + sudoku[0][2]);
else if (col == N)
helper(sudoku, row + 1, 0, usedNums);
else if (sudoku[row][col] != 0)
helper(sudoku, row, col + 1, usedNums);
else {
for (int val : rangeC(1, N))
if (usedNums.canPlace(row, col, val)) {
sudoku[row][col] = val;
usedNums.update(row, col, val, true);
helper(sudoku, row, col + 1, usedNums);
usedNums.update(row, col, val, false);
}
sudoku[row][col] = 0;
}
}
class UsedNums {
boolean[][] usedByRow = new boolean[N + 1][N + 1];
boolean[][] usedByCol = new boolean[N + 1][N + 1];
boolean[][] usedByBox = new boolean[N + 1][N + 1];
void update(int row, int col, int val, boolean isUsed) {
usedByRow[row][val] = isUsed;
usedByCol[col][val] = isUsed;
usedByBox[getBox(row, col)][val] = isUsed;
}
boolean canPlace(int row, int col, int val) {
return !usedByRow[row][val] && !usedByCol[col][val] && !usedByBox[getBox(row, col)][val];
}
int getBox(int row, int col) {
return iroundDown(row, K) + col / K;
}
}
}