Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create N_Queen's_position_Printing_in_grid_style #463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// N chess queens on an N×N chessboard so that no two queens attack each other

//By Soumyadeep Pal
#include <iostream>
using namespace std;

//For counting total possible soln
static int count;

// Queen's position Printing in grid style
void print_Grid_style(int userRows, int **arr)
{
int userColumns = userRows;
cout << "\n";
cout << " ";
int i, j;
cout << "\n";
for (i = 0; i <= 2 * userRows; i++)
{
if (i % 2 != 0)
cout << " ";
for (j = 0; j <= 2 * userColumns; j++)
{
if (i % 2 == 0)
{
if (j == 0)
cout << " ";
if (j % 2 == 0)
cout << " ";
else
cout << "---";
}
else
{
if (j % 2 == 0)
cout << "|";
else
{
cout << " ";
if (arr[i / 2][j / 2] == 1)
cout << "Q"; //just replace Q to 1 &
else
cout << " "; // replace space to 0 if u want to print in 0-1 format
cout << " ";
}
}
}
cout << "\n";
}
}

bool check_pos(int **arr, int x, int y, int n)
{
for (int row = 0; row < n; row++)
{
if (arr[row][y] == 1)
{
return false;
}
}

//for upper left diagonal
int row = x, col = y;
while (row >= 0 && col >= 0)
{
if (arr[row][col] == 1)
{
return false;
}
row--, col--;
}

//for upper right diagonal
row = x, col = y;
while (row >= 0 && col < n) //coz queen can't be placed outside
{
if (arr[row][col] == 1)
{
return false;
}
row--, col++;
}
return true;
}
bool nqueen(int **arr, int x, int n) //not required to pass y & prev column coz we already placed our queen on the next row
{
//Base case:
if (x >= n)
{
print_Grid_style(n, arr);
count++;
}

for (int col = 0; col < n; col++)
{
if (check_pos(arr, x, col, n))
{
arr[x][col] = 1;

if (nqueen(arr, x + 1, n))
{
return true;
}
//If placed queen clashes with already placed queens then remove queen from board and put 0 in those position
arr[x][col] = 0;
}
}
return false;
}

int main()
{
cout << "Enter the number of Queens: ";
int n;
cin >> n;
int **arr = new int *[n];

for (int i = 0; i < n; i++)
{

arr[i] = new int[n];
}

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{

arr[i][j] = 0;
}
}
if (n > 3)
{
nqueen(arr, 0, n);
}
cout << "\nPossible solutions exist for an " << n << "-queen problem: " << count << "\n";
return 0;
}