-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1D_Array_8Queens_NoGoto.cpp
72 lines (60 loc) · 1.46 KB
/
1D_Array_8Queens_NoGoto.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
/*Objective: Find all Possible placements of Queens on an 8x8 board
such that no Queens are killed by another queens with No Goto
*/
#include <iostream>
#include<cstdlib>
#include <cmath>
using namespace std;
bool ok(int q[], int col){
for(int i = 0; i < col; i++){
//Checks if there is a queen on same row or coloumn.
if(q[i] == q[col] || (col-i) == abs(q[col]-q[i])){
return false;
}
}
return true;
}
void backtrack(int &col){
col--;
if(col == -1)
exit(1);
}
void print(int q[]){
for(int c = 0; c < 8; c++){
cout << q[c];
}
cout << endl;
}
int main(){
int q[8]; q[0]=0;
int solution= 0;
/*board setup section*/
int c=0;
bool from_backtrack=false;
while(true){
//next col
while(c<8){
if(!from_backtrack){
c++;
if(c==8)
break;
q[c] = -1;
}
from_backtrack=false;
while(q[c]<8){
q[c]++;
if(q[c] == 8){
backtrack(c);
from_backtrack=true;
}
if(ok(q, c))
break;
}
}
cout << "solution# " <<++solution << endl;
print(q);
cout << endl;
backtrack(c);
from_backtrack=true;
}
}