-
Notifications
You must be signed in to change notification settings - Fork 0
/
RecursiveSolver.cpp
39 lines (29 loc) · 973 Bytes
/
RecursiveSolver.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
//
// Created by Armin on 13/04/2023.
//
#include "RecursiveSolver.h"
RecursiveSolver::RecursiveSolver(const std::string &input) : SudokuSolver(input) {
}
void recursiveSolve(std::shared_ptr<SudokuMove> const & move, std::list<std::shared_ptr<SudokuMove>> & solutions, int x, int y) {
if (move->isSolved()) {
#pragma omp critical
solutions.push_back(move);
return;
}
move->findNextAllowedMove(x, y);
if (x > 9 || y > 9)
return; // no more empty cells
for (int value = 1; value <= 9; ++value) {
if (move->isMoveAllowed(x, y, value)) {
{
auto nextMove = std::make_shared<SudokuMove>(move, x, y, value);
recursiveSolve(nextMove, solutions, x, y);
}
}
}
}
std::list<std::shared_ptr<SudokuMove>> RecursiveSolver::solve() {
std::list<std::shared_ptr<SudokuMove>> solutions;
recursiveSolve(baseMove, solutions, 0, 0);
return solutions;
}