Skip to content

Commit

Permalink
added a condition for n<3 as a solution does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkSpy25 committed Oct 4, 2022
1 parent c6cdbff commit 44d9785
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions CPP/recursion/N_Queens/N_Queens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
#include <vector>

// Function to print the bord
void printBoard(std::vector<std::vector<int>>& board, int n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 1) {
void printBoard(std::vector<std::vector<int>> &board, int n)
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (board[i][j] == 1)
{
std::cout << 'Q' << ' ';
}
else {
else
{
std::cout << '_' << ' ';
}
}
Expand All @@ -17,11 +22,14 @@ void printBoard(std::vector<std::vector<int>>& board, int n) {
}

// Function for checking valid squares.
bool canBePlaced(std::vector<std::vector<int>>& board, int i, int j, int n) {
bool canBePlaced(std::vector<std::vector<int>> &board, int i, int j, int n)
{

// Vertical check.
for (int v = 0; v < i; ++v) {
if (board[v][j] == 1) {
for (int v = 0; v < i; ++v)
{
if (board[v][j] == 1)
{
return false;
}
}
Expand All @@ -30,17 +38,21 @@ bool canBePlaced(std::vector<std::vector<int>>& board, int i, int j, int n) {
int u = i, v = j;

// Upper left diagonal check.
while (i >= 0 && j >= 0) {
if(board[i][j] == 1) {
while (i >= 0 && j >= 0)
{
if (board[i][j] == 1)
{
return false;
}
--i;
--j;
}

// Upper right diagonal.
while (u >= 0 && v < n) {
if (board[u][v] == 1) {
while (u >= 0 && v < n)
{
if (board[u][v] == 1)
{
return false;
}
--u;
Expand All @@ -52,11 +64,13 @@ bool canBePlaced(std::vector<std::vector<int>>& board, int i, int j, int n) {
}

// Main recursive function.
bool N_Queens(std::vector<std::vector<int>>& board, int n, int row) {
bool N_Queens(std::vector<std::vector<int>> &board, int n, int row)
{
// --> base case
// When row becomes equal to n
// means all queens have been placed in the right manner.
if (row == n) {
if (row == n)
{
// Print the first possible way.
printBoard(board, n);
// Endl for printing the next possible board
Expand All @@ -68,20 +82,24 @@ bool N_Queens(std::vector<std::vector<int>>& board, int n, int row) {
}

// --> recursive case
for (int column = 0; column < n; ++column) {
for (int column = 0; column < n; ++column)
{
// Check if the queen can be placed at the ith row and jth column.
if (canBePlaced(board, row, column, n)) {
if (canBePlaced(board, row, column, n))
{
// Mark that place as 1.
board[row][column] = 1;
// Check for further positions.
bool remaining_positions = N_Queens(board, n, row + 1);
if (remaining_positions) {
if (remaining_positions)
{
// If queens can be placed in the remaining positions
// it means the placing of queens can proceed further.
// return true
return true;
}
else {
else
{
// If queens cannot be placed further in the right way
// backtrack to the previous position.
board[row][column] = 0;
Expand All @@ -92,18 +110,22 @@ bool N_Queens(std::vector<std::vector<int>>& board, int n, int row) {
return false;
}

int main() {
int main()
{

int n = 0;
// Input Size of the chessboard
// No of queens will be the same as size n.
std::cin >> n;

if (n <= 3)
{
std::cout << "No solution exists for n = " << n << std::endl;
}
// 2D vector for storing board having size max size of nXn.
// First create a column vector of size n.
std::vector<int> column(n);
// Create Row vectors and fill rows with columns.
std::vector<std::vector<int>> board(n,column);
std::vector<std::vector<int>> board(n, column);

// Function call for
// Arguments passed are : 2D vector board
Expand Down
Binary file added CPP/recursion/N_Queens/N_Queens.exe
Binary file not shown.

0 comments on commit 44d9785

Please sign in to comment.