forked from ProAlgos/ProAlgos-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
n_queens.cpp
97 lines (79 loc) · 2.15 KB
/
n_queens.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
/*
N-Queens problem
----------------
Find a way to place N non-attacking queens on an N×N chessboard
Time complexity
---------------
O(N!), where N is the number of queens
Space complexity
----------------
O(1), constant amount of extra space
*/
#include <iostream>
using namespace std;
const int MaxQueens = 40;
bool board[MaxQueens][MaxQueens];
void initBoard(const size_t size) {
size_t r, c;
for (r = 0; r < size; r++)
for (c = 0; c < size; c++)
board[r][c] = false;
}
void showBoard(const size_t size) {
size_t r, c;
for (r = 0; r < size; r++) {
for (c = 0; c < size; c++) {
if (board[r][c])
cout << 'Q';
else
cout << '.';
cout << ' ';
}
cout << '\n';
}
}
bool isSafe(const int row, const int col, const int size) {
int r, c;
// check along the column
for (r = 0; r < size; r++)
if (board[r][col])
return false;
// check along the row
for (c = 0; c < col; c++)
if (board[row][c])
return false;
// check diagonally
for (c = col-1, r = row-1; c >= 0 && r >= 0; c--, r--)
if (board[r][c])
return false;
for (c = col-1, r = row+1; c >= 0 && r < size; c--, r++)
if (board[r][c])
return false;
return true;
}
bool canPlaceQueens(const size_t col, const size_t size) {
if (col == size)
return true;
for (size_t row = 0; row < size; row++) {
if (isSafe(row, col, size)) {
board[row][col] = true;
if (canPlaceQueens(col+1, size))
return true;
board[row][col] = false;
}
}
return false;
}
int main() {
int N; // number of queens to place = N, size of board = NxN
cout << "Enter the number of queens to place (max " << MaxQueens << ") : ";
cin >> N;
initBoard(N);
if (canPlaceQueens(0, N)) {
cout << "Found a way!\n";
showBoard(N);
}
else
cout << "Couldn\'t find a way to place " << N << " queens on a " << N << "x" << N << " board\n";
return 0;
}