-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParallelSudoku.java
61 lines (42 loc) · 1.85 KB
/
ParallelSudoku.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
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
import java.util.concurrent.RecursiveTask;
class ParallelSudoku implements SudokuAnalyser {
private SudokuBoard board;
ParallelSudoku(SudokuBoard board) { this.board = board; }
@Override
public int computeSolutionsNumber() { return new ForkJoinPool().invoke(new RecursiveSolver(board)); }
class RecursiveSolver extends RecursiveTask<Integer> {
SudokuBoard board;
RecursiveSolver(SudokuBoard board) { this.board = board; }
@Override
protected Integer compute() {
// sequential cutoff
if (board.completedPercentage() > 80.0)
return new SequentialSudoku(board).computeSolutionsNumber();
List<RecursiveSolver> tasks = new ArrayList<>();
// get empty cell with less candidates
byte cell = board.getNextCell();
// recursively try every candidate for that cell
for (byte val: board.getCandidates(cell)) {
SudokuBoard newBoard = board.clone();
// if new configuration is valid
if (newBoard.insert(val, cell)) tasks.add(new RecursiveSolver(newBoard));
}
if (tasks.size() == 0) return 0;
// keep one task to be run by this
RecursiveSolver onThis = tasks.remove(0);
// fork other tasks
tasks.forEach(t -> t.fork());
// compute this
int out = onThis.compute();
// sum solutions from other tasks
return tasks.stream()
.map(t -> t.join())
.reduce(out, Integer::sum);
}
}
}