-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fancy_8Queens.cpp
132 lines (113 loc) · 4.55 KB
/
Fancy_8Queens.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
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
bool ok(int q[], int c) {
for (int i = 0; i < c; ++i)
if (q[i] == q[c] || abs(q[i] - q[c]) == c - i)
return false;
return true;
}
void backtrack(int &col){
col--;
if(col == -1)
exit(1);
}
void print(int q[]) {
int i, j, k, l;
typedef char box[5][7]; // box is now a data type: a 5x7 2D array of characters
box bb, wb, *board[8][8]; // bb and wb are boxes (5x7 arrays). board is an 8x8 array of pointers to boxes.
// You don't have to clean up the board after printing, because it's not static,
// so it'll be reinitialized the next time you call the funtion.
// Fill in bb (black box) and wb (white box). They each represent a square of the chessboard.
// You only need to create one of each, since the chessboard can contain many pointers to the same box.
for (i=0; i<5; i++)
for (j=0; j<7; j++) {
bb[i][j] = ' ';
wb[i][j] = char(219);
}
// Create 2 more boxes to represent the queens, by drawing a picture of each queen in the 2D array.
static box bq = { {char(219),char(219),char(219),char(219),char(219),char(219),char(219)},
{char(219),' ' ,char(219),' ' ,char(219),' ' ,char(219)},
{char(219),' ' ,' ' ,' ' ,' ' ,' ' ,char(219)},
{char(219),' ' ,' ' ,' ' ,' ' ,' ' ,char(219)},
{char(219),char(219),char(219),char(219),char(219),char(219),char(219)} };
static box wq = { {' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' },
{' ' ,char(219),' ' ,char(219),' ' ,char(219),' ' },
{' ' ,char(219),char(219),char(219),char(219),char(219),' ' },
{' ' ,char(219),char(219),char(219),char(219),char(219),' ' },
{' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' } };
// Fill board with pointers to bb and wb in alternate positions.
for(i=0; i<8; i++)
for(j=0; j<8; j++)
if((i+j)%2 == 0)
board[i][j] = &wb;
else
board[i][j] = &bb;
//q[k] is the row the queen is in, k is the column the queen is in
//if the column + row where queen is in is even then bq else wq
//board[q[k][k] = the location of the queen on the board.
for(int k =0; k < 8; k++){
if((k+q[k])%2 == 0)
board[q[k]][k] = &bq;
else
board[q[k]][k] = &wq;
}
// Set up the current solution on the chessboard by placing pointers to bq and wq in the appropriate squares.
// Place black queens on white squares and white queens on black squares, so that you will be able to see them when printing.
// Write the code.
// Print upper border
for (i=0; i<7*8; i++)
cout << '_';
cout << "\n";
// Print the board
for (i=0; i<8; i++) // for each board row
for (k=0; k<5; k++) { // for each box row
cout << char(179);
for (j=0; j<8; j++) // for each board column
for (l=0; l<7; l++) // for each box column
// board[i][j] is the box pointer in the ith row, jth column of the board.
// *board[i][j] is the box being pointed to.
// (*board[i][j])[k][l] is the kth row, lth column of the box.
cout << (*board[i][j])[k][l];
cout << char(179) << "\n";
}
// Print lower border
cout << " ";
for (i=0; i<7*8; i++)
cout << char(196);
cout << "\n\n";
}
int main(){
int q[8]; q[0]=0;
int solution= 1;
/*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) ;
solution = solution + 1;
backtrack(c);
from_backtrack=true;
}
}