-
Notifications
You must be signed in to change notification settings - Fork 3
/
N-queens-puzzle.cpp
181 lines (163 loc) · 4.12 KB
/
N-queens-puzzle.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @file 8-queens-puzzle.cpp
* @author prakash ([email protected])
* @brief
* @version 0.1
* @date 2021-08-01
*
* @copyright Copyright (c) 2021
*
*/
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
class EightQueensPuzzle {
public:
int n;
std::vector<std::vector<int>> board;
std::vector<std::vector<int>> slashDiagnol, backSlashDiagnol;
std::vector<bool> rowLookup, slashDiagnolLook, backslashDiagnolLook;
EightQueensPuzzle(int n_) : n(n_) {
board.resize(n);
for (int i = 0; i < n; i++) {
board[i].resize(n);
for (int j = 0; j < n; j++) {
board[i][j] = 0;
}
}
slashDiagnol.resize(n);
backSlashDiagnol.resize(n);
for (int i = 0; i < n; i++) {
slashDiagnol[i].resize(n);
backSlashDiagnol[i].resize(n);
for (int j = 0; j < n; j++) {
slashDiagnol[i][j] = 0;
backSlashDiagnol[i][j] = 0;
}
}
rowLookup.resize(n, false);
slashDiagnolLook.resize(2 * n - 1, false);
backslashDiagnolLook.resize(2 * n - 1, false);
// initialize helper matrices
for (int r = 0; r < n; r++)
for (int c = 0; c < n; c++) {
slashDiagnol[r][c] = r + c, backSlashDiagnol[r][c] = r - c + 7;
}
}
bool isSafe(int row, int col) {
int i, j;
/* check row */
for (i = 0; i < n; i++) {
if (board[row][i] == 1) {
return false;
}
}
/* check column */
for (i = 0; i < n; i++) {
if (board[i][col] == 1) {
return false;
}
}
/* check upper diagonal */
for (i = row, j = col; i < n && j < n; i++, j++) {
if (board[i][j] == 1) {
return false;
}
}
/* check lower diagonal */
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 1) {
return false;
}
}
return true;
}
bool backtrackNqueens(int col) {
if (col >= n) {
return true;
}
for (int i = 0; i < n; i++) {
if (isSafe(i, col)) {
board[i][col] = 1;
if (backtrackNqueens(col + 1)) {
return true;
}
board[i][col] = 0;
}
}
return false;
}
void solve_with_backtrack() {
if (!backtrackNqueens(0)) {
std::cout << "No solution" << std::endl;
} else {
printSolution();
}
}
void solve_with_branch_and_bound() {
if (!recursion_with_branch_and_bound(0)) {
std::cout << "No solution" << std::endl;
} else {
printSolution();
}
}
bool isBranchandBoundPossible(int row, int col) {
if (slashDiagnolLook[slashDiagnol[row][col]] ||
backslashDiagnolLook[backSlashDiagnol[row][col]] || rowLookup[row])
return false;
else
return true;
}
bool recursion_with_branch_and_bound(int col) {
if (col >= n) {
return true;
}
for (int i = 0; i < n; i++) {
if (isBranchandBoundPossible(i, col)) {
slashDiagnolLook[slashDiagnol[i][col]] = true;
backslashDiagnolLook[backSlashDiagnol[i][col]] = true;
rowLookup[i] = true;
board[i][col] = 1;
if (recursion_with_branch_and_bound(col + 1)) {
return true;
}
slashDiagnolLook[slashDiagnol[i][col]] = false;
backslashDiagnolLook[backSlashDiagnol[i][col]] = false;
rowLookup[i] = false;
board[i][col] = 0;
}
}
return false;
}
void reset() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = 0;
}
}
}
void printSolution() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
std::cout << board[i][j] << " ";
}
std::cout << std::endl;
}
}
};
int main(int argc, const char **argv) {
EightQueensPuzzle p(8);
std::cout << "Backtracking" << std::endl;
time_t start = time(&start);
p.solve_with_backtrack();
time_t end = time(&end);
std::cout << " took " << end - start << " seconds" << std::endl;
p.reset();
std::cout << "Branch and Bound" << std::endl;
start = time(&start);
p.solve_with_branch_and_bound();
end = time(&end);
std::cout << " took " << end - start << " seconds" << std::endl;
return 0;
}