-
Notifications
You must be signed in to change notification settings - Fork 0
/
HybridSolver.cpp
62 lines (43 loc) · 1.72 KB
/
HybridSolver.cpp
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
//
// Created by Armin on 13/04/2023.
//
#include <vector>
#include "HybridSolver.h"
#include "RecursiveSolver.h"
HybridSolver::HybridSolver(std::string const & input, int magicNumber) : SudokuSolver(input), magicNumber(magicNumber) {
}
bool preprocessLayer(std::vector<std::shared_ptr<SudokuMove>> & collection, std::shared_ptr<SudokuMove> const & move, int x, int y) {
move->findNextAllowedMove(x, y);
if (x >= 9 || y >= 9) {
collection.push_back(move);
return false;
}
for (int value = 1; value <= 9; ++value) {
if (move->isMoveAllowed(x, y, value)) {
collection.push_back(std::make_shared<SudokuMove>(move, x, y, value));
}
}
return true;
}
std::vector<std::shared_ptr<SudokuMove>> preprocess(std::shared_ptr<SudokuMove> const & baseMove, int magicNumber, int x, int y) {
std::vector<std::shared_ptr<SudokuMove>> preprocessedMoves;
preprocessedMoves.push_back(baseMove);
bool canGenerateMore = true;
while (preprocessedMoves.size() < magicNumber && canGenerateMore) {
std::vector<std::shared_ptr<SudokuMove>> temp;
for (auto & move : preprocessedMoves) {
canGenerateMore &= preprocessLayer(temp, move, x, y);
}
preprocessedMoves = std::move(temp);
}
return preprocessedMoves;
}
std::list<std::shared_ptr<SudokuMove>> HybridSolver::solve() {
std::list<std::shared_ptr<SudokuMove>> solutions;
std::vector<std::shared_ptr<SudokuMove>> preprocessedMoves = preprocess(baseMove, magicNumber, 0, 0);
#pragma omp parallel for default(none) shared(preprocessedMoves, solutions)
for (auto & move : preprocessedMoves) {
recursiveSolve(move, solutions, 0, 0);
}
return solutions;
}