2
2
#include < vector>
3
3
4
4
// Function to print the bord
5
- void printBoard (std::vector<std::vector<int >>& board, int n) {
6
- for (int i = 0 ; i < n; ++i) {
7
- for (int j = 0 ; j < n; ++j) {
8
- if (board[i][j] == 1 ) {
5
+ void printBoard (std::vector<std::vector<int >> &board, int n)
6
+ {
7
+ for (int i = 0 ; i < n; ++i)
8
+ {
9
+ for (int j = 0 ; j < n; ++j)
10
+ {
11
+ if (board[i][j] == 1 )
12
+ {
9
13
std::cout << ' Q' << ' ' ;
10
14
}
11
- else {
15
+ else
16
+ {
12
17
std::cout << ' _' << ' ' ;
13
18
}
14
19
}
@@ -17,11 +22,14 @@ void printBoard(std::vector<std::vector<int>>& board, int n) {
17
22
}
18
23
19
24
// Function for checking valid squares.
20
- bool canBePlaced (std::vector<std::vector<int >>& board, int i, int j, int n) {
25
+ bool canBePlaced (std::vector<std::vector<int >> &board, int i, int j, int n)
26
+ {
21
27
22
28
// Vertical check.
23
- for (int v = 0 ; v < i; ++v) {
24
- if (board[v][j] == 1 ) {
29
+ for (int v = 0 ; v < i; ++v)
30
+ {
31
+ if (board[v][j] == 1 )
32
+ {
25
33
return false ;
26
34
}
27
35
}
@@ -30,17 +38,21 @@ bool canBePlaced(std::vector<std::vector<int>>& board, int i, int j, int n) {
30
38
int u = i, v = j;
31
39
32
40
// Upper left diagonal check.
33
- while (i >= 0 && j >= 0 ) {
34
- if (board[i][j] == 1 ) {
41
+ while (i >= 0 && j >= 0 )
42
+ {
43
+ if (board[i][j] == 1 )
44
+ {
35
45
return false ;
36
46
}
37
47
--i;
38
48
--j;
39
49
}
40
50
41
51
// Upper right diagonal.
42
- while (u >= 0 && v < n) {
43
- if (board[u][v] == 1 ) {
52
+ while (u >= 0 && v < n)
53
+ {
54
+ if (board[u][v] == 1 )
55
+ {
44
56
return false ;
45
57
}
46
58
--u;
@@ -52,11 +64,13 @@ bool canBePlaced(std::vector<std::vector<int>>& board, int i, int j, int n) {
52
64
}
53
65
54
66
// Main recursive function.
55
- bool N_Queens (std::vector<std::vector<int >>& board, int n, int row) {
67
+ bool N_Queens (std::vector<std::vector<int >> &board, int n, int row)
68
+ {
56
69
// --> base case
57
70
// When row becomes equal to n
58
71
// means all queens have been placed in the right manner.
59
- if (row == n) {
72
+ if (row == n)
73
+ {
60
74
// Print the first possible way.
61
75
printBoard (board, n);
62
76
// Endl for printing the next possible board
@@ -68,20 +82,24 @@ bool N_Queens(std::vector<std::vector<int>>& board, int n, int row) {
68
82
}
69
83
70
84
// --> recursive case
71
- for (int column = 0 ; column < n; ++column) {
85
+ for (int column = 0 ; column < n; ++column)
86
+ {
72
87
// Check if the queen can be placed at the ith row and jth column.
73
- if (canBePlaced (board, row, column, n)) {
88
+ if (canBePlaced (board, row, column, n))
89
+ {
74
90
// Mark that place as 1.
75
91
board[row][column] = 1 ;
76
92
// Check for further positions.
77
93
bool remaining_positions = N_Queens (board, n, row + 1 );
78
- if (remaining_positions) {
94
+ if (remaining_positions)
95
+ {
79
96
// If queens can be placed in the remaining positions
80
97
// it means the placing of queens can proceed further.
81
98
// return true
82
99
return true ;
83
100
}
84
- else {
101
+ else
102
+ {
85
103
// If queens cannot be placed further in the right way
86
104
// backtrack to the previous position.
87
105
board[row][column] = 0 ;
@@ -92,18 +110,22 @@ bool N_Queens(std::vector<std::vector<int>>& board, int n, int row) {
92
110
return false ;
93
111
}
94
112
95
- int main () {
113
+ int main ()
114
+ {
96
115
97
116
int n = 0 ;
98
117
// Input Size of the chessboard
99
118
// No of queens will be the same as size n.
100
119
std::cin >> n;
101
-
120
+ if (n <= 3 )
121
+ {
122
+ std::cout << " No solution exists for n = " << n << std::endl;
123
+ }
102
124
// 2D vector for storing board having size max size of nXn.
103
125
// First create a column vector of size n.
104
126
std::vector<int > column (n);
105
127
// Create Row vectors and fill rows with columns.
106
- std::vector<std::vector<int >> board (n,column);
128
+ std::vector<std::vector<int >> board (n, column);
107
129
108
130
// Function call for
109
131
// Arguments passed are : 2D vector board
0 commit comments