-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBfsSolver.hpp
More file actions
56 lines (50 loc) · 1.7 KB
/
BfsSolver.hpp
File metadata and controls
56 lines (50 loc) · 1.7 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
#pragma once
#include "Board.hpp"
#include <vector>
#include <unordered_set>
Board solveBFS(size_t levelNr, Board initialBoard) {
std::vector<MoveSequence> queueThis;
std::vector<MoveSequence> queueNext;
std::unordered_set<uint64_t> seen;
queueThis.push_back(initialBoard.moveSequence);
size_t steps = 0;
while (!queueThis.empty()) {
MoveSequence sequence = queueThis.back();
queueThis.pop_back();
Board board = initialBoard;
for (size_t i = 0; i < sequence.n; i++) {
board.click(sequence.moves[i].row, sequence.moves[i].col);
}
for (size_t row = 0; row < rows; row++) {
for (size_t col = 0; col < cols; col++) {
if (!board.fields[row][col].isClickable()) {
continue;
}
Board newBoard = board;
newBoard.click(row, col);
if (newBoard.isSolved()) {
return newBoard;
} else {
uint64_t code = newBoard.hash();
if (!seen.contains(code)) {
queueNext.push_back(newBoard.moveSequence);
seen.emplace(code);
}
}
}
}
if (queueThis.empty()) {
queueThis = queueNext;
queueNext.clear();
steps++;
if (steps > 5) {
std::cout<<"# Calculating solutions for "<<levelNr<<", currently at "
<<steps<<" steps. Queue length: "<<queueThis.size()<<std::endl;
}
if (steps >= maxSteps) {
return {};
}
}
}
return {};
}