Skip to content

Commit 54745c7

Browse files
authored
Add N-Queens problem solution using backtracking
Implement N-Queens problem using backtracking with safety checks for queen placements.
1 parent b37d439 commit 54745c7

1 file changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,116 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
=====================================================
6+
Algorithm: N-Queens Problem using Backtracking
7+
=====================================================
8+
9+
The N-Queens problem is to place N queens on an N×N chessboard
10+
such that no two queens attack each other. A queen can attack
11+
horizontally, vertically, and diagonally.
12+
13+
Approach:
14+
1. Place queens one column at a time (from left to right).
15+
2. For each column, try placing a queen in every row:
16+
- Check if the position is safe (no conflict in row, upper-left diagonal, lower-left diagonal).
17+
- If safe, place the queen and recursively attempt to place the rest.
18+
- If placing the queen leads to no solution, backtrack (remove the queen and try next row).
19+
3. Stop when all queens are successfully placed.
20+
21+
=====================================================
22+
Time Complexity:
23+
- For each column, we try placing a queen in N rows.
24+
- At each step, we perform an O(N) check for safety (row + 2 diagonals).
25+
- Worst-case time complexity: O(N!) (factorial growth).
26+
- More precisely: O(N * N!) due to the safety check overhead.
27+
28+
Space Complexity:
29+
- O(N^2) for the chessboard representation.
30+
- O(N) recursive call stack (depth = number of columns).
31+
=====================================================
32+
*/
33+
34+
// Function to print the solution board
35+
void printSolution(vector<vector<int>>& board) {
36+
int n = board.size();
37+
for (int row = 0; row < n; row++) {
38+
for (int col = 0; col < n; col++) {
39+
if (board[row][col] == 1)
40+
cout << "Q "; // Queen placed
41+
else
42+
cout << ". "; // Empty cell
43+
}
44+
cout << "\n";
45+
}
46+
}
47+
48+
// Check if it's safe to place a queen at board[row][col]
49+
bool isSafe(vector<vector<int>>& board, int row, int col) {
50+
int n = board.size();
51+
52+
// Check row on the left
53+
for (int j = 0; j < col; j++)
54+
if (board[row][j] == 1)
55+
return false;
56+
57+
// Check upper-left diagonal
58+
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
59+
if (board[i][j] == 1)
60+
return false;
61+
62+
// Check lower-left diagonal
63+
for (int i = row, j = col; i < n && j >= 0; i++, j--)
64+
if (board[i][j] == 1)
65+
return false;
66+
67+
return true; // Safe to place queen
68+
}
69+
70+
// Recursive utility function to solve N-Queens
71+
bool solveNQUtil(vector<vector<int>>& board, int col) {
72+
int n = board.size();
73+
74+
// Base case: all queens are placed
75+
if (col >= n)
76+
return true;
77+
78+
// Try placing queen in each row of current column
79+
for (int row = 0; row < n; row++) {
80+
if (isSafe(board, row, col)) {
81+
// Place queen
82+
board[row][col] = 1;
83+
84+
// Recur to place rest of the queens
85+
if (solveNQUtil(board, col + 1))
86+
return true;
87+
88+
// Backtrack: remove queen
89+
board[row][col] = 0;
90+
}
91+
}
92+
93+
// No valid placement in this column
94+
return false;
95+
}
96+
97+
// Solve N-Queens problem
98+
bool solveNQ(int n) {
99+
vector<vector<int>> board(n, vector<int>(n, 0));
100+
101+
if (!solveNQUtil(board, 0)) {
102+
cout << "Solution does not exist";
103+
return false;
104+
}
105+
106+
printSolution(board);
107+
return true;
108+
}
109+
110+
// Driver code
111+
int main() {
112+
int n = 4;
113+
solveNQ(n);
114+
return 0;
115+
}
1116

0 commit comments

Comments
 (0)