-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFancy Eight Queens Problem.cpp
119 lines (93 loc) · 2.57 KB
/
Fancy Eight Queens Problem.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<iostream>
using namespace std;
bool ok(int q[], int column){
for(int i=0; i<column; i++){
if((q[i] == q[column]) || (abs(q[column] - q[i]) == (column - i)))
return false;
}
return true;
}
void Backtrack(int &column){
column--;
if(column == -1) exit(1);
}
void print(int q[]) {
static int counter =1;
cout<< endl << "Solution #" << counter++ << endl;
int i, j, k, l;
typedef char box[5][7];
box bb,wb,*board[8][8];
char ws = char(219); // char 219 is black box
box bq = { // Crown for black boxes
ws, ws, ws, ws, ws, ws, ws,
ws, ' ', ws, ' ', ws, ' ', ws,
ws, ' ', ' ', ' ', ' ', ' ', ws,
ws, ' ', ' ', ' ', ' ', ' ', ws,
ws, ws, ws, ws, ws, ws, ws
};
box wq = { // Crown for white boxes
' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ws, ' ', ws, ' ', ws, ' ',
' ', ws, ws, ws, ws, ws, ' ',
' ', ws, ws, ws, ws, ws, ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' '
};
for(i=0; i<5; i++) //create the black boxes
for(j=0; j<7; j++){
wb[i][j] = ' ';
bb[i][j] = char(219);
}
for(i=0; i<8 ;i++)
for(j=0; j<8; j++)
if((i+j) % 2 == 0) {
if(q[i] == j) {
board[i][j] = &wq;
} else { board[i][j] = &wb;}}
else {
if(q[i] == j){
board[i][j] = &bq;}
else {
board[i][j] = &bb;}
}
cout << " ";
for(i=0; i<7*8; i++) // Upper Border
cout << '_';
cout << endl;
for(i=0; i<8; i++) // Printing board
for(k=0; k<5; k++){
cout << " " << char(179); //Left border
for(j=0; j<8; j++)
for(l=0; l<7; l++)
cout << (*board[i][j])[k][l];
cout << char(179) << endl;
}
cout << " ";
for(i=0; i<7*8; i++) // Lower Border
cout << char(196);
cout << endl;
}
int main(){
int q[8]; q[0]=0;
int c=1;
bool _backtrack=false;
while(1){
while(c<8){
if(!_backtrack)
q[c]=-1;
_backtrack=false;
while(q[c]<8){
q[c]++;
while(q[c]==8){
Backtrack(c);
q[c]++;}
if(ok(q, c))
break;
}
c++;
}
print(q);
Backtrack(c);
_backtrack=true;
}
return 0;
}