forked from pawanrajsingh2088/cpp-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN-Queen.cpp
More file actions
68 lines (55 loc) · 1.77 KB
/
N-Queen.cpp
File metadata and controls
68 lines (55 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
using namespace std;
// Function to print the chessboard
void printBoard(vector<vector<int>>& board, int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << (board[i][j] ? "Q " : ". ");
}
cout << endl;
}
cout << endl;
}
// Function to check if a queen can be placed safely
bool isSafe(vector<vector<int>>& board, int row, int col, int N) {
// Check column
for (int i = 0; i < row; i++)
if (board[i][col]) return false;
// Check upper left diagonal
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j]) return false;
// Check upper right diagonal
for (int i = row, j = col; i >= 0 && j < N; i--, j++)
if (board[i][j]) return false;
return true;
}
// Recursive function to solve N-Queens
bool solveNQueens(vector<vector<int>>& board, int row, int N) {
// Base case: all queens are placed
if (row == N) {
printBoard(board, N);
return true; // print one solution, can continue to find others
}
bool foundSolution = false;
// Try placing queen in all columns of current row
for (int col = 0; col < N; col++) {
if (isSafe(board, row, col, N)) {
board[row][col] = 1; // place queen
foundSolution = solveNQueens(board, row + 1, N) || foundSolution;
board[row][col] = 0; // backtrack
}
}
return foundSolution;
}
int main() {
int N;
cout << "Enter number of queens: ";
cin >> N;
vector<vector<int>> board(N, vector<int>(N, 0));
if (!solveNQueens(board, 0, N))
cout << "No solution exists for " << N << " queens.\n";
else
cout << "All possible solutions are shown above.\n";
return 0;
}