Skip to content

Commit 44d9785

Browse files
committed
added a condition for n<3 as a solution does not exist
1 parent c6cdbff commit 44d9785

File tree

2 files changed

+43
-21
lines changed

2 files changed

+43
-21
lines changed

CPP/recursion/N_Queens/N_Queens.cpp

+43-21
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
#include <vector>
33

44
// 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+
{
913
std::cout << 'Q' << ' ';
1014
}
11-
else {
15+
else
16+
{
1217
std::cout << '_' << ' ';
1318
}
1419
}
@@ -17,11 +22,14 @@ void printBoard(std::vector<std::vector<int>>& board, int n) {
1722
}
1823

1924
// 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+
{
2127

2228
// 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+
{
2533
return false;
2634
}
2735
}
@@ -30,17 +38,21 @@ bool canBePlaced(std::vector<std::vector<int>>& board, int i, int j, int n) {
3038
int u = i, v = j;
3139

3240
// 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+
{
3545
return false;
3646
}
3747
--i;
3848
--j;
3949
}
4050

4151
// 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+
{
4456
return false;
4557
}
4658
--u;
@@ -52,11 +64,13 @@ bool canBePlaced(std::vector<std::vector<int>>& board, int i, int j, int n) {
5264
}
5365

5466
// 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+
{
5669
// --> base case
5770
// When row becomes equal to n
5871
// means all queens have been placed in the right manner.
59-
if (row == n) {
72+
if (row == n)
73+
{
6074
// Print the first possible way.
6175
printBoard(board, n);
6276
// Endl for printing the next possible board
@@ -68,20 +82,24 @@ bool N_Queens(std::vector<std::vector<int>>& board, int n, int row) {
6882
}
6983

7084
// --> recursive case
71-
for (int column = 0; column < n; ++column) {
85+
for (int column = 0; column < n; ++column)
86+
{
7287
// 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+
{
7490
// Mark that place as 1.
7591
board[row][column] = 1;
7692
// Check for further positions.
7793
bool remaining_positions = N_Queens(board, n, row + 1);
78-
if (remaining_positions) {
94+
if (remaining_positions)
95+
{
7996
// If queens can be placed in the remaining positions
8097
// it means the placing of queens can proceed further.
8198
// return true
8299
return true;
83100
}
84-
else {
101+
else
102+
{
85103
// If queens cannot be placed further in the right way
86104
// backtrack to the previous position.
87105
board[row][column] = 0;
@@ -92,18 +110,22 @@ bool N_Queens(std::vector<std::vector<int>>& board, int n, int row) {
92110
return false;
93111
}
94112

95-
int main() {
113+
int main()
114+
{
96115

97116
int n = 0;
98117
// Input Size of the chessboard
99118
// No of queens will be the same as size n.
100119
std::cin >> n;
101-
120+
if (n <= 3)
121+
{
122+
std::cout << "No solution exists for n = " << n << std::endl;
123+
}
102124
// 2D vector for storing board having size max size of nXn.
103125
// First create a column vector of size n.
104126
std::vector<int> column(n);
105127
// 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);
107129

108130
// Function call for
109131
// Arguments passed are : 2D vector board

CPP/recursion/N_Queens/N_Queens.exe

96.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)