-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnQueens.cpp
More file actions
64 lines (53 loc) · 1.81 KB
/
nQueens.cpp
File metadata and controls
64 lines (53 loc) · 1.81 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
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 4;
void printSolution(vector<int>& board, int n) {
cout << "Solution:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i] == j)
cout << "Q ";
else
cout << ". ";
}
cout << endl;
}
cout << endl;
}
bool isSafe(vector<int>& board, int row, int col, int n) {
for (int i = 0; i < row; i++) {
if (board[i] == col || // Same column
board[i] - i == col - row || // Same diagonal (top-left to bottom-right)
board[i] + i == col + row) // Same diagonal (top-right to bottom-left)
return false;
}
return true;
}
void solveNQueens(int n, int row, vector<int>& board, vector<vector<int>>& solutions) {
if (row == n) {
solutions.push_back(board); // Store the solution
printSolution(board, n); // Print the solution
return;
}
for (int col = 0; col < n; col++) {
if (isSafe(board, row, col, n)) {
board[row] = col; // Place the queen
solveNQueens(n, row + 1, board, solutions); // Solve for the next row
board[row] = -1; // Backtrack
}
}
}
int main() {
int n;
cout << "Enter the value of n (1 <= n <= 4): ";
cin >> n;
if (n > MAX_N || n < 1) {
cout << "Invalid input. Please enter a value between 1 and 4." << endl;
return 0;
}
vector<int> board(n, -1); // Initialize the board
vector<vector<int>> solutions; // Store all solutions
solveNQueens(n, 0, board, solutions);
cout << "Total Solutions: " << solutions.size() << endl;
return 0;
}