diff --git a/N_Queen.cpp b/N_Queen.cpp new file mode 100644 index 0000000..09c07d4 --- /dev/null +++ b/N_Queen.cpp @@ -0,0 +1,43 @@ +#include +#include +using namespace std; + +bool isSafe(const vector>& board, int row, int col, int N) { + for (int i = 0; i < col; i++) + if (board[row][i]) return false; + for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) + if (board[i][j]) return false; + for (int i = row, j = col; i < N && j >= 0; i++, j--) + if (board[i][j]) return false; + return true; +} + +bool solveNQueensUtil(vector>& board, int col, int N) { + if (col >= N) return true; + for (int i = 0; i < N; i++) { + if (isSafe(board, i, col, N)) { + board[i][col] = 1; + if (solveNQueensUtil(board, col + 1, N)) return true; + board[i][col] = 0; + } + } + return false; +} + +void solveNQueens(int N) { + vector> board(N, vector(N, 0)); + if (solveNQueensUtil(board, 0, N)) { + for (const auto& row : board) { + for (int cell : row) cout << (cell ? "Q " : ". "); + cout << endl; + } + } else { + cout << "Solution does not exist" << endl; + } +} + +int main() { + int N = 4; // Change N to solve for different sizes + solveNQueens(N); + return 0; +}