15 #ifndef PAAL_N_QUEENS_SOLUTION_HPP
16 #define PAAL_N_QUEENS_SOLUTION_HPP
20 #include <boost/iterator/counting_iterator.hpp>
21 #include <boost/range/iterator_range.hpp>
22 #include <boost/range/numeric.hpp>
36 typedef typename boost::counting_iterator<int> QueensIterator;
44 : m_queen_position(pos),
45 m_numeber_attacing_diagonal_negative(boost::distance(pos), 0),
46 m_numeber_attacing_diagonal_nonnegative(boost::distance(pos), 0),
47 m_numeber_attacing_counter_diagonal(2 * boost::distance(pos), 0) {
48 for (
auto q :
irange(boost::distance(pos))) {
58 QueensIterator
begin()
const {
return QueensIterator(0); }
65 QueensIterator
end()
const {
66 return QueensIterator(m_queen_position.size());
76 int leftPosition = m_queen_position[xLeft];
77 put_queen(xLeft, m_queen_position[xRight]);
78 put_queen(xRight, leftPosition);
90 return m_numeber_attacing_counter_diagonal[x + y] + get_diagonal(x, y);
100 int get_y(
int x)
const {
return m_queen_position[x]; }
108 auto attacingNr = [](
int sum,
int n) {
return sum + n * (n - 1) / 2; };
109 int sum = boost::accumulate(m_numeber_attacing_counter_diagonal, 0,
111 sum = boost::accumulate(m_numeber_attacing_diagonal_negative, sum,
113 return boost::accumulate(m_numeber_attacing_diagonal_nonnegative, sum,
125 void put_queen(
int x,
int y) {
127 m_queen_position[x] = y;
138 int &get_diagonal(
int x) {
return get_diagonal(x, m_queen_position[x]); }
148 int &get_diagonal(
int x,
int y) {
150 return m_numeber_attacing_diagonal_negative[x - y];
152 return m_numeber_attacing_diagonal_nonnegative[y - x];
164 int get_diagonal(
int x,
int y)
const {
166 return m_numeber_attacing_diagonal_negative[x - y];
168 return m_numeber_attacing_diagonal_nonnegative[y - x];
177 void decrease(
int x) {
178 --m_numeber_attacing_counter_diagonal[x + m_queen_position[x]];
187 void increase(
int x) {
188 ++m_numeber_attacing_counter_diagonal[x + m_queen_position[x]];
192 NQueensPositionsVector &m_queen_position;
193 std::vector<int> m_numeber_attacing_diagonal_negative;
194 std::vector<int> m_numeber_attacing_diagonal_nonnegative;
195 std::vector<int> m_numeber_attacing_counter_diagonal;
201 #endif // PAAL_N_QUEENS_SOLUTION_HPP
QueensIterator begin() const
begin of the queens positions' collection
Adapts vector representing n queen problem to class able to efficiently compute gain of given move...
auto irange(T begin, T end)
irange
int obj_fun() const
computes total number of conflicts on the board
n_queens_solution_adapter(NQueensPositionsVector &pos)
constructor
int get_num_attacing(int x, int y) const
get number of queens attacing (x,y) position
int get_y(int x) const
return y for xth queen
QueensIterator end() const
end of the queens positions' collection
bool local_search(Solution &solution, SearchStrategy searchStrategy, ContinueOnSuccess succ, ContinueOnFail fail, components...comps)
detail
void swap_queens(int xLeft, int xRight)
swaps two queens positions