forked from subailong/datalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnqueens.cxx
98 lines (81 loc) · 2.01 KB
/
nqueens.cxx
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// nqueens.h
// datalgo
// Created by Iyed Bennour on 07/12/2014.
// Copyright (c) 2014 Iyed Bennour. All rights reserved.
//
#include <iostream>
#include <vector>
#include <sstream>
#include <set>
#include <cmath>
std::set<std::vector<int>> solve(int n)
{
class place_queens
{
int queens;
public:
place_queens(int _n): queens(_n){}
std::set<std::vector<int>> operator()(int moves)
{
if(moves == 0) return std::set<std::vector<int>>{{}};
else {
std::set<std::vector<int>> solutions = this->operator()(moves - 1);
std::set<std::vector<int>> nexts;
for (auto v : solutions) {
for (auto col = 0; col < queens; ++col) {
if (is_safe(col, v)) {
std::vector<int> next(v.size());
std::copy(v.begin(), v.end(), next.begin());
next.push_back(col);
nexts.insert(next);
}
}
}
return nexts;
}
}
bool is_safe(int col, const std::vector<int>& positions)
{
int row = positions.size();
if (positions.empty()) {
return true;
}
else {
for(int i = 0; i < positions.size(); ++i){
if( col == positions[i]
|| std::abs(row - i) == std::abs(col - positions[i]) ) {
return false;
}
}
}
return true;
}
};
return place_queens(n)(n);
}
bool test_nqueens()
{
auto is_solution = [&](const std::vector<int>& positions) {
for (int i = 0; i < positions.size(); ++i) {
for (int j = 0; j < positions.size(); ++j){
if (i != j) {
if (positions[i] == positions[j]){
return false;
}
if(std::abs(i - j) == std::abs(positions[i] - positions[j])) {
return false;
}
}
}
}
return true;
};
bool ret = true;
std::set<std::vector<int>> solutions = solve(8);
for (auto &e: solutions) {
if (!is_solution(e))
ret = false;
}
return ret;
}