From 3e2a36ca4486a909678544bdc09b6295cef64570 Mon Sep 17 00:00:00 2001 From: Soumyadeep Pal <64325089+soumyadeep2002ss@users.noreply.github.com> Date: Sun, 17 Oct 2021 00:35:32 +0530 Subject: [PATCH] Create N_Queen's_position_Printing_in_grid_style Enter the number of Queens: 4 --- --- --- --- | | Q | | | --- --- --- --- | | | | Q | --- --- --- --- | Q | | | | --- --- --- --- | | | Q | | --- --- --- --- --- --- --- --- | | | Q | | --- --- --- --- | Q | | | | --- --- --- --- | | | | Q | --- --- --- --- | | Q | | | --- --- --- --- Possible solutions exist for an 4-queen problem: 2 --- ...nting_in_grid_style(All_Possible_Soln).cpp | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 Algorithms/C++/C++/N_Queen's_position_Printing_in_grid_style(All_Possible_Soln).cpp diff --git a/Algorithms/C++/C++/N_Queen's_position_Printing_in_grid_style(All_Possible_Soln).cpp b/Algorithms/C++/C++/N_Queen's_position_Printing_in_grid_style(All_Possible_Soln).cpp new file mode 100644 index 00000000..b6e4c400 --- /dev/null +++ b/Algorithms/C++/C++/N_Queen's_position_Printing_in_grid_style(All_Possible_Soln).cpp @@ -0,0 +1,138 @@ +// N chess queens on an N×N chessboard so that no two queens attack each other + +//By Soumyadeep Pal +#include +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; +}