Repository navigation
Expand file tree
/
Copy pathsolver.cpp
More file actions
65 lines (51 loc) · 2.17 KB
/
Copy pathsolver.cpp
File metadata and controls
65 lines (51 loc) · 2.17 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
#include "solver.hpp"
#include <iostream>
using Solution = std::vector<std::pair<int, int>>;
Solver::Solver(int stars) : stars(stars) {}
void Solver::backtrack(int n, int starsPlaced, std::vector<std::vector<int>> regions,
std::vector<int>& remainingInRow, std::vector<int>& remainingInColumn, std::vector<int>& remainingInRegion,
Solution& currSolution, std::vector<Solution>& solutions) {
if(n * stars == starsPlaced) {
solutions.push_back(currSolution);
return;
}
auto adj = adjacentCells(currSolution);
// Naive backtracking just go row by row
auto row = starsPlaced;
for(std::size_t col = 0; col < regions[row].size(); col++) {
auto region = regions[row][col];
if(remainingInRow[row] > 0 && remainingInColumn[col] > 0 && remainingInRegion[region - 1] > 0 && adj.count(std::make_pair(row, col)) == 0) {
remainingInRow[row]--;
remainingInColumn[col]--;
remainingInRegion[region - 1]--;
currSolution.push_back(std::make_pair(row, col));
backtrack(n, starsPlaced + 1, regions, remainingInRow, remainingInColumn, remainingInRegion, currSolution, solutions);
remainingInRow[row]++;
remainingInColumn[col]++;
remainingInRegion[region - 1]++;
currSolution.pop_back();
}
}
}
std::vector<Solution> Solver::solve(Puzzle p) {
auto n = p.size();
auto regions = p.board();
std::vector<int> remainingInRow(n, stars);
std::vector<int> remainingInColumn(n, stars);
std::vector<int> remainingInRegion(n, stars);
Solution currSolution;
std::vector<Solution> solutions;
backtrack(n, 0, regions, remainingInRow, remainingInColumn, remainingInRegion, currSolution, solutions);
return solutions;
}
std::unordered_set<std::pair<int, int>, pairHash> Solver::adjacentCells(std::vector<std::pair<int, int>> cells) {
std::unordered_set<std::pair<int, int>, pairHash> adj;
for(auto p : cells) {
auto row = p.first;
auto col = p.second;
for(auto r = -1; r <= 1; r++) {
for(auto c = -1; c <= 1; c++) adj.insert(std::make_pair(row + r, col + c));
}
}
return adj;
}