-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
80 lines (65 loc) · 1.58 KB
/
main.cpp
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
#include <iostream>
#include <vector>
#include "nqueens.h"
#include "checkPositions.h"
#include "makeBoard.h"
#include <string>
using namespace std;
int N = 11;
int main(){
vector<Queen> queens;
int count = 0;
int row = 1;
int col = 1;
while(queens.size()<N){
while(queens.size()<N){
Queen new_q(row, col);
queens.push_back(new_q);
if((diagonal(queens, N) && across(queens)) || (queens.size() == 1)){
++col;
row = 1;
}
else{
if(row <= N){
Queen last_queen = queens.at(queens.size()-1);
row = last_queen.getRow()+1;
}
while(row > N && queens.size() > 1){
queens.pop_back();
Queen last_queen = queens.at(queens.size()-1);
row = last_queen.getRow()+1;
col = last_queen.getColumn();
}
queens.pop_back();
if(row == N+1 && col == 1){
if(count > 0){
cout << "An " << N << "x" << N << " grid has " << count << " possible formations" << endl;
return 0;
}else{
cout << "Impossible to solve with a " << N << "x" << N << " grid." << endl;
return 0;
}
}
}//end else there was a hit
}//end while
for(vector<Queen>::const_iterator it = queens.begin(); it != queens.end(); ++it)
cout << *it << endl;
cout << "_------------------------------" << endl;
vector<Queen> ordered;
ordered.reserve(queens.size());
vector<Queen>::iterator it = orderByRow(queens, ordered);
Queen q2;
for(int i = 0; i < N; ++i){
q2 = *(it+i);
cout << createRow(q2.getColumn(), N) << endl;
}
++count;
do{
Queen last_queen = queens.at(queens.size()-1);
queens.pop_back();
row = last_queen.getRow()+1;
col = last_queen.getColumn();
}while(row > N);
}
cout << "An " << N << "x" << N << " grid has " << count << " possible formations" << endl;
}