-
Notifications
You must be signed in to change notification settings - Fork 0
/
8queen1D.cpp
56 lines (49 loc) · 1.06 KB
/
8queen1D.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
#include <iostream>
#include <cmath>
using namespace std;
// test if previous queens conflict with current colum
bool ok(int q[], int c) {
for(int i = 1;i <= c; i ++){
if(q[c] == q[i-1] || q[c-i] == q[c]-i || q[c-i] == q[c]+i)
return false;
}
return true;
}
void print(int q[]) {
static int numSolutions = 0;
cout << "Solution #" << ++numSolutions << ": ";
// Print the array
for(int i = 0; i < 8; i++)
cout << q[i];
cout << "\n";
}
int main() {
//start array
int q[8]={0};
int r = 0, c = 0;
//next colum then print if its the last colum
while (c >= 0){
c++;
if(c == 8){
print(q);
c--;
r = q[c];
}
else
r = -1;
//next row
while (c >= 0) {
r++;
q[c] = r;
if(r == 8){
c --;
r = q[c];
}
//test queen
else if(ok(q,c)){
break;
}
}
}
return 0;
}