Skip to content

Commit 554af98

Browse files
committed
Add: [n-queens] in [c++]
1 parent 29ee2fa commit 554af98

1 file changed

Lines changed: 64 additions & 59 deletions

File tree

CPP/recursion/n_queen_problem.cpp

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,104 @@
11
#include <bits/stdc++.h>
22
using namespace std;
33

4-
// A utility function to print solution
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
535
void printSolution(vector<vector<int>>& board) {
636
int n = board.size();
7-
for (int i = 0; i < n; i++) {
8-
for (int j = 0; j < n; j++)
9-
if(board[i][j])
10-
cout << "Q ";
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
1141
else
12-
cout << ". ";
42+
cout << ". "; // Empty cell
43+
}
1344
cout << "\n";
1445
}
1546
}
1647

17-
// A utility function to check if a queen can
18-
// be placed on board[row][col]. Note that this
19-
// function is called when "col" queens are
20-
// already placed in columns from 0 to col -1.
21-
// So we need to check only left side for
22-
// attacking queens
23-
bool isSafe(vector<vector<int>>& board,
24-
int row, int col) {
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) {
2550
int n = board.size();
26-
int i, j;
2751

28-
// Check this row on left side
29-
for (i = 0; i < col; i++)
30-
if (board[row][i])
52+
// Check row on the left
53+
for (int j = 0; j < col; j++)
54+
if (board[row][j] == 1)
3155
return false;
3256

33-
// Check upper diagonal on left side
34-
for (i = row, j = col; i >= 0 &&
35-
j >= 0; i--, j--)
36-
if (board[i][j])
57+
// Check upper-left diagonal
58+
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
59+
if (board[i][j] == 1)
3760
return false;
3861

39-
// Check lower diagonal on left side
40-
for (i = row, j = col; j >= 0 &&
41-
i < n; i++, j--)
42-
if (board[i][j])
62+
// Check lower-left diagonal
63+
for (int i = row, j = col; i < n && j >= 0; i++, j--)
64+
if (board[i][j] == 1)
4365
return false;
4466

45-
return true;
67+
return true; // Safe to place queen
4668
}
4769

48-
// A recursive utility function to solve N
49-
// Queen problem
70+
// Recursive utility function to solve N-Queens
5071
bool solveNQUtil(vector<vector<int>>& board, int col) {
5172
int n = board.size();
52-
53-
// base case: If all queens are placed
54-
// then return true
73+
74+
// Base case: all queens are placed
5575
if (col >= n)
5676
return true;
5777

58-
// Consider this column and try placing
59-
// this queen in all rows one by one
60-
for (int i = 0; i < n; i++) {
61-
62-
// Check if the queen can be placed on
63-
// board[i][col]
64-
if (isSafe(board, i, col)) {
65-
66-
// Place this queen in board[i][col]
67-
board[i][col] = 1;
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;
6883

69-
// recur to place rest of the queens
84+
// Recur to place rest of the queens
7085
if (solveNQUtil(board, col + 1))
7186
return true;
7287

73-
// If placing queen in board[i][col]
74-
// doesn't lead to a solution, then
75-
// remove queen from board[i][col]
76-
board[i][col] = 0; // BACKTRACK
88+
// Backtrack: remove queen
89+
board[row][col] = 0;
7790
}
7891
}
7992

80-
// If the queen cannot be placed in any row in
81-
// this column col then return false
93+
// No valid placement in this column
8294
return false;
8395
}
8496

85-
// This function solves the N Queen problem using
86-
// Backtracking. It mainly uses solveNQUtil() to
87-
// solve the problem. It returns false if queens
88-
// cannot be placed, otherwise, return true and
89-
// prints placement of queens in the form of 1s.
90-
// Please note that there may be more than one
91-
// solutions, this function prints one of the
92-
// feasible solutions.
97+
// Solve N-Queens problem
9398
bool solveNQ(int n) {
9499
vector<vector<int>> board(n, vector<int>(n, 0));
95100

96-
if (solveNQUtil(board, 0) == false) {
101+
if (!solveNQUtil(board, 0)) {
97102
cout << "Solution does not exist";
98103
return false;
99104
}
@@ -102,7 +107,7 @@ bool solveNQ(int n) {
102107
return true;
103108
}
104109

105-
// Driver program to test above function
110+
// Driver code
106111
int main() {
107112
int n = 4;
108113
solveNQ(n);

0 commit comments

Comments
 (0)