Added K-Knights and Count-N-queens

pull/854/head
Ashad001 2022-09-29 15:02:57 +05:00
parent b67f5e1786
commit dc1185576b
3 changed files with 185 additions and 1 deletions

View File

@ -0,0 +1,69 @@
// Time Complexity: O(N!)
#include <iostream>
#include <vector>
using namespace std;
bool Check(vector<vector<int>> &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<vector<int>> &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<vector<int>> arr(n, vector<int>(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

View File

@ -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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void Display(vector<vector<int>> &arr)
{
for (auto row : arr)
{
for (auto num : row)
{
if (num)
cout << "K ";
else
cout << "x ";
}
cout << endl;
}
cout << endl;
}
bool IsValid(vector<vector<int>> &arr, int row, int col)
{
if (row >= 0 && row < arr.size() && col >= 0 && col < arr.size())
{
return true;
}
return false;
}
bool IsSafe(vector<vector<int>> &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<vector<int>> &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<vector<int>> 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;
}

View File

@ -206,3 +206,5 @@
## Backtracking ## Backtracking
- [N-Queens Problem](Backtracking/n-queens.cpp) - [N-Queens Problem](Backtracking/n-queens.cpp)
- [Count N-Queens](Backtracking/count-n-queens.cpp)
- [K-Knights Problem](Backtracking/k-knights.cpp)