From dc1185576b0f7cfaeefde21ec81b4e635071b49f Mon Sep 17 00:00:00 2001 From: Ashad001 <93534298+Ashad001@users.noreply.github.com> Date: Thu, 29 Sep 2022 15:02:57 +0500 Subject: [PATCH] Added K-Knights and Count-N-queens --- .../CPlusPlus/Backtracking/count-n-queens.cpp | 69 +++++++++++ .../CPlusPlus/Backtracking/k-knights.cpp | 113 ++++++++++++++++++ algorithms/CPlusPlus/README.md | 4 +- 3 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 algorithms/CPlusPlus/Backtracking/count-n-queens.cpp create mode 100644 algorithms/CPlusPlus/Backtracking/k-knights.cpp diff --git a/algorithms/CPlusPlus/Backtracking/count-n-queens.cpp b/algorithms/CPlusPlus/Backtracking/count-n-queens.cpp new file mode 100644 index 00000000..aa311c13 --- /dev/null +++ b/algorithms/CPlusPlus/Backtracking/count-n-queens.cpp @@ -0,0 +1,69 @@ +// Time Complexity: O(N!) +#include +#include +using namespace std; + +bool Check(vector> &arr, int row, int col) +{ + // Check column + int i{0}, j{0}; + for (i = 0; i < row; i++) + { + if (arr[i][col] == 1) + { + return false; + } + } + // Diagonals + for (i = row, j = col; i >= 0 && j >= 0; i--, j--) + { + if (arr[i][j] == 1) + { + return false; + } + } + for (i = row, j = col; i >= 0 && j < arr.size(); i--, j++) + { + if (arr[i][j] == 1) + { + return false; + } + } + return true; +} +int countQueens(vector> &arr, int row) +{ + if (row >= arr.size()) + { + return 1; + } + + int count = 0; + for (int i = 0; i < arr.size(); i++) + { + if (Check(arr, row, i) == 1) + { + arr[row][i] = 1; + count += countQueens(arr, row + 1); + arr[row][i] = 0; + } + + } + return count; +} + +void totalNQueens(int n) +{ + vector> arr(n, vector(n, 0)); + cout << countQueens(arr, 0); +} + +int main(int argc, char const *argv[]) +{ + totalNQueens(5); // Checks how many queens can be placed in a 5 x 5 board + + return 0; +} + + +// Output: 10 \ No newline at end of file diff --git a/algorithms/CPlusPlus/Backtracking/k-knights.cpp b/algorithms/CPlusPlus/Backtracking/k-knights.cpp new file mode 100644 index 00000000..d8f14665 --- /dev/null +++ b/algorithms/CPlusPlus/Backtracking/k-knights.cpp @@ -0,0 +1,113 @@ +// Time Complexity : O(8^(n^2)) +// There are N2 Cells and for each, we have a maximum of 8 possible moves to choose from +#include +#include +#include +using namespace std; + +void Display(vector> &arr) +{ + for (auto row : arr) + { + for (auto num : row) + { + if (num) + cout << "K "; + else + cout << "x "; + } + cout << endl; + } + cout << endl; +} +bool IsValid(vector> &arr, int row, int col) +{ + if (row >= 0 && row < arr.size() && col >= 0 && col < arr.size()) + { + return true; + } + return false; +} +bool IsSafe(vector> &arr, int row, int col) +{ + if (IsValid(arr, row - 2, col - 1) == 1) + { + if (arr[row - 2][col - 1] == 1) + return false; + } + if (IsValid(arr, row - 2, col + 1) == 1) + { + if (arr[row - 2][col + 1] == 1) + return false; + } + if (IsValid(arr, row + 1, col + 2) == 1) + { + if (arr[row + 1][col + 2] == 1) + return false; + } + if (IsValid(arr, row - 1, col + 2) == 1) + { + if (arr[row - 1][col + 2] == 1) + return false; + } + if (IsValid(arr, row + 2, col - 1) == 1) + { + if (arr[row + 2][col - 1] == 1) + return false; + } + if (IsValid(arr, row + 2, col + 1) == 1) + { + if (arr[row + 2][col + 1] == 1) + return false; + } + if (IsValid(arr, row + 1, col - 2) == 1) + { + if (arr[row + 1][col - 2] == 1) + return false; + } + if (IsValid(arr, row - 1, col - 2) == 1) + { + if (arr[row - 1][col - 2] == 1) + return false; + } + return true; +} + +void Knight(vector> &board, int row, int col, int knights) +{ + if (knights == 0) // If all knights are places than show the board + { + Display(board); + return; + } + if (row == board.size() - 1 && col == board.size()) + { + return; // out of bounds + } + if (col == board.size()) + { + Knight(board, row + 1, 0, knights); // switching rows + return; + } + if (IsSafe(board, row, col)) + { + board[row][col] = true; // Placing the knight + Knight(board, row, col + 1, knights - 1); + board[row][col] = false; // Backtraking + } + Knight(board, row, col + 1, knights); +} + +int main(int argc, char const *argv[]) +{ + vector> arr = + { + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}, + {0, 0, 0, 0}}; + // Display(arr); + Knight(arr, 0, 0, 5); + + return 0; +} diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 6a713e39..defa7b34 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -205,4 +205,6 @@ ## Backtracking -- [N-Queens Problem](Backtracking/n-queens.cpp) \ No newline at end of file +- [N-Queens Problem](Backtracking/n-queens.cpp) +- [Count N-Queens](Backtracking/count-n-queens.cpp) +- [K-Knights Problem](Backtracking/k-knights.cpp) \ No newline at end of file