-
Notifications
You must be signed in to change notification settings - Fork 0
/
nqueenvisualizer.cpp
361 lines (337 loc) · 9.64 KB
/
nqueenvisualizer.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <unistd.h>
using namespace std;
class NQueensVisualizer
{
private:
vector<vector<int>> board;
int n;
int total_no_of_solutions = 0;
int solution_no = 0;
string state = "Solving";
int nextSol = -1;
/**
* @brief Prints the current state of the board.
*
* The 'state' variable determines whether to print the board in the "Solving" or "Solved" format.
* In the "Solving" format, the board is displayed with queens represented by " ⇯ " and empty squares represented by " ▣ "
* In the "Solved" format, the board is displayed with queens represented by " ⇯ " and empty squares represented by " ▢ "
*/
void display()
{
vector<string> temp;
string s;
if (state == "Solved")
{
for (int i = 0; i < n; i++)
{
s = "";
for (int j = 0; j < n; j++)
{
if (board[i][j] == 1)
{
s += " ⇯ ";
}
else
{
s += "▢ ";
}
}
temp.push_back(s);
}
}
else
{
for (int i = 0; i < n; i++)
{
s = "";
for (int j = 0; j < n; j++)
{
if (board[i][j] == 1)
{
s += " ⇯ ";
}
else if (board[i][j] < 0)
{
s += " ▣ ";
}
else
{
s += " ▢ ";
}
}
temp.push_back(s);
}
}
for (int i = 0; i < n; i++)
{
cout << temp[i] << endl;
cout << endl;
}
}
/**
* Checks if the position (i, j) on the board is safe for placing a queen.
* A position is considered safe if there are no conflicts with previously
* placed queens in the same column, row, or diagonals.
*
* @param row The row index to check.
* @param col The column index to check.
* @return True if the position is safe; otherwise, false.
*/
bool isSafe(int row, int col)
{
return (board[row][col] == 0);
}
/**
* Attempts to solve the N-Queens problem starting from a given row.
* Uses a backtracking approach to place queens on the board such that
* no two queens threaten each other.
*
* @param row The current row to attempt placing a queen.
* @return True if a solution is found; otherwise, false.
*/
bool solve(int row)
{
if (row == n)
{
sleep(1);
state = "Solved";
cout << "\033[2J\033[H";
cout << "Solution: " << endl
<< endl;
display();
solution_no++;
nextSol = -1;
return true;
}
for (int j = 0; j < n; j++)
{
if (isSafe(row, j))
{
board[row][j] = 1;
for (int k = row + 1; k < n; k++)
{
board[k][j] += -1;
}
for (int i = 0; i < n; i++)
{
if (board[row][i] == 1)
{
continue;
}
board[row][i] += -1;
}
int i = row + 1;
int k = j + 1;
while (k < n && i < n)
{
board[i][k] += -1;
i++;
k++;
}
i = row + 1;
k = j - 1;
while (i < n && k >= 0)
{
board[i][k] += -1;
i++;
k--;
}
state = "Solving";
sleep(1);
cout << "\033[2J\033[H";
cout << "Solving..." << endl
<< endl;
display();
if (solve(row + 1))
{
if (solution_no < total_no_of_solutions)
{
if (nextSol == -1)
{
cout << "Press 1 to get next solution or 0 to stop: ";
cin >> nextSol;
}
if (nextSol == 0)
{
return true;
}
}
else
{
return true;
}
}
for (int k = row + 1; k < n; k++)
{
board[k][j] += 1;
}
for (int i = 0; i < n; i++)
{
if (board[row][i] == 1)
{
continue;
}
board[row][i] += 1;
}
i = row + 1;
k = j + 1;
while (k < n && i < n)
{
board[i][k] += 1;
i++;
k++;
}
i = row + 1;
k = j - 1;
while (i < n && k >= 0)
{
board[i][k] += 1;
i++;
k--;
}
board[row][j] = 0;
state = "Solving";
sleep(1);
cout << "\033[2J\033[H";
cout << "Solving..." << endl
<< endl;
display();
}
}
return false;
}
/**
* @brief Recursively counts the number of solutions of the N-Queens problem.
* @param row The current row to attempt placing a queen.
* @return True if a solution is found; otherwise, false.
*/
void countSolutions(int row)
{
if (row == n)
{
total_no_of_solutions++;
return;
}
for (int j = 0; j < n; j++)
{
if (isSafe(row, j))
{
board[row][j] = 1;
for (int k = row + 1; k < n; k++)
{
board[k][j] += -1;
}
for (int i = 0; i < n; i++)
{
if (board[row][i] == 1)
{
continue;
}
board[row][i] += -1;
}
int i = row + 1;
int k = j + 1;
while (k < n && i < n)
{
board[i][k] += -1;
i++;
k++;
}
i = row + 1;
k = j - 1;
while (i < n && k >= 0)
{
board[i][k] += -1;
i++;
k--;
}
countSolutions(row + 1);
for (int k = row + 1; k < n; k++)
{
board[k][j] += 1;
}
for (int i = 0; i < n; i++)
{
if (board[row][i] == 1)
{
continue;
}
board[row][i] += 1;
}
i = row + 1;
k = j + 1;
while (k < n && i < n)
{
board[i][k] += 1;
i++;
k++;
}
i = row + 1;
k = j - 1;
while (i < n && k >= 0)
{
board[i][k] += 1;
i++;
k--;
}
board[row][j] = 0;
}
}
return;
}
public:
/**
* @brief Solves the NQueens problem with backtracking and prints the solution
* @param n the size of the board
*
* This function reads the number of queens from the user, initializes an instance of the NQueensVisualizer
* class, and calls the solveNQueens method to find and display the solution to the N-Queens problem.
*/
void solveNQueens(int n)
{
board.clear();
board = vector<vector<int>>(n, vector<int>(n, 0));
this->n = n;
total_no_of_solutions = 0;
solution_no = 0;
state = "Solving";
nextSol = -1;
countSolutions(0);
if (solve(0) == false)
{
cout << "No solution exists" << endl;
}
else
{
cout << "\033[2J\033[H";
cout << "Solution: " << endl
<< endl;
display();
}
nextSol = -1;
state = "Solving";
total_no_of_solutions = 0;
solution_no = 0;
this->n = 0;
board.clear();
}
};
/**
* @brief Main function that prompts the user for the number of queens and solves the N-Queens problem.
*
* This function reads the number of queens from the user, initializes an instance of the NQueensVisualizer
* class, and calls the solveNQueens method to find and display the solution to the N-Queens problem.
*/
int main()
{
int n;
cout << "Enter the number of queens: ";
cin >> n;
NQueensVisualizer nqv;
nqv.solveNQueens(n);
return 0;
}