Sudoku Solver 1.0
Загрузка...
Поиск...
Не найдено
dancing_links_matrix.h
См. документацию.
1#pragma once
2
3#include <vector>
4#include <stack>
5#include <span>
6#include <optional>
7
8class DancingLinksMatrix final {
9private:
10 // Forward declarations
11 struct Node;
12
13public:
18 explicit DancingLinksMatrix(int num_columns);
19
24
27
31 DancingLinksMatrix(DancingLinksMatrix&& other) noexcept;
32
37
43 void addRow(std::span<const int> col_indices, int row_id);
44
49 void cover(int col_idx);
50
55 void uncover(int col_idx);
56
62 std::optional<std::vector<int>> search(int expected_solution_size = 0);
63
69 bool isCovered(int col_idx) const;
70
75 bool isFullyUncovered() const;
76
80 int countColumns() const;
81
82#ifdef DEBUG
86 void print() const;
87#endif // DEBUG
88
89private:
90
91 void cover_impl(Node* col);
92 void uncover_impl(Node* col);
93 bool search_impl(std::vector<int>& solution);
94
95 // Откат всех cover (для cleanup)
96 void rollbackAll();
97
98 Node* getColumn(int idx) const;
99
105 bool isCovered(Node* col) const;
106
111 Node* chooseColumn() const;
112
116 void cleanup();
117
118 Node* root;
119 std::vector<Node*> columns;
120 std::stack<Node*> cover_stack; // стек вызовов cover (для проверки порядка)
121
122};