-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFancy 8 Queens.cpp
More file actions
111 lines (92 loc) · 3.79 KB
/
Fancy 8 Queens.cpp
File metadata and controls
111 lines (92 loc) · 3.79 KB
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
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
//Mohammed Chowdhury
//CS 211-22C
//Assignment #11
bool ok(int q[], int c);
void print(int q[]);
void next(int q[], int c);
int main(){
int q[8];
next(q, 0);
return 0;
}
//..................FUNCTIONS................//
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 print(int q[]) {
static int solution = 0;
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;
// 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.
//***
cout << "Solution #" << ++solution << ":\n ";
for (i = 0; i <8;i++)
if ( (q[i]+i) %2 == 0 )
board[q[i]][i] = &bq;
else board[q[i]][i] = &wq;
// 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";
}
void next(int q[], int c) {
if (c == 8)
print(q);
else for (q[c] = 0; q[c] < 8; ++q[c])
if (ok(q, c))
next(q, c+1);
}