-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNQueenRecursion.cpp
More file actions
75 lines (59 loc) · 1.95 KB
/
NQueenRecursion.cpp
File metadata and controls
75 lines (59 loc) · 1.95 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
69
70
71
72
73
74
75
#include <iostream>
#include <vector>
using namespace std;
int N; // Size of the chessboard
vector<vector<char>> board; // Chessboard represented as a 2D grid
// Function to print one solution
void printSolution() {
cout << "One solution for " << N << "-Queens problem:\n";
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
}
// Function to check if a queen placement is safe
bool isSafe(int row, int col) {
// Check the column
for (int i = 0; i < row; i++) {
if (board[i][col] == 'Q') return false;
}
// Check the upper-left diagonal
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 'Q') return false;
}
// Check the upper-right diagonal
for (int i = row - 1, j = col + 1; i >= 0 && j < N; i--, j++) {
if (board[i][j] == 'Q') return false;
}
return true;
}
// Recursive function to solve the N-Queens problem row by row
bool solve(int row) {
if (row == N) { // All queens are placed successfully
printSolution();
return true;
}
for (int col = 0; col < N; col++) {
if (isSafe(row, col)) {
board[row][col] = 'Q'; // Place the queen
if (solve(row + 1)) { // Try to place queens in the next row
return true; // Stop when a solution is found
}
board[row][col] = '.'; // Backtrack: remove the queen
}
}
return false; // No solution found in this configuration
}
int main() {
cout << "Enter the number of queens: ";
cin >> N;
// Initialize the board with empty spaces (.)
board = vector<vector<char>>(N, vector<char>(N, '.'));
// Try to find one solution
if (!solve(0)) {
cout << "No solution found for " << N << "-Queens problem.\n";
}
return 0;
}