Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions 51.N-Queens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <bits/stdc++.h>
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Avoid using <bits/stdc++.h> for portability.

This header is not part of the C++ standard and can cause compilation issues on non-GCC compilers. Please include only the specific headers you need.

Suggested implementation:

#include <vector>
#include <string>
using namespace std;

If your code uses input/output streams (e.g., cout, cin), you should also add #include <iostream>. If you use any algorithms (e.g., sort, find), add #include <algorithm>. Review the rest of your file and include only the headers you actually use.

using namespace std;

class Solution {
public:
// Function to check if placing a queen at (row, col) is safe
bool isSafe(int row, int col, vector<string> &temp, int n) {
int duprow = row;
int dupcol = col;
// Check all columns to the left in the same row
for (int j = 0; j < col; j++) {
if (temp[row][j] == 'Q') return false;
}

// Check upper-left diagonal
while(row >= 0 && col >= 0){
if(temp[row][col] == 'Q') return false;
Comment on lines +16 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Diagonal checks can be replaced with auxiliary arrays for efficiency.

Tracking diagonal occupancy with arrays enables constant-time checks, which is more efficient for large boards than iterating through each cell.

Suggested implementation:

    // Function to check if placing a queen at (row, col) is safe using auxiliary arrays
    bool isSafe(int row, int col, vector<int> &leftRow, vector<int> &upperDiagonal, vector<int> &lowerDiagonal, int n) {
        // Check if the column, upper diagonal, or lower diagonal is occupied
        if (leftRow[col] || lowerDiagonal[row + col] || upperDiagonal[n - 1 + col - row])
            return false;
        return true;
  1. You need to declare and initialize the auxiliary arrays in your main/backtracking function:
    • vector<int> leftRow(n, 0);
    • vector<int> upperDiagonal(2 * n - 1, 0);
    • vector<int> lowerDiagonal(2 * n - 1, 0);
  2. When placing a queen, set the corresponding entries to 1:
    • leftRow[col] = 1;
    • lowerDiagonal[row + col] = 1;
    • upperDiagonal[n - 1 + col - row] = 1;
  3. When removing a queen (backtracking), reset them to 0.
  4. Update all calls to isSafe to pass the new arrays.

row--;
col--;
}
row = duprow;
col = dupcol;
// Check lower-left diagonal
while(row < n && col >= 0){
if(temp[row][col] == 'Q') return false;
row++;
col--;
}

return true;
}

// Backtracking function to place queens column by column
void solve(int col, vector<string> temp, vector<vector<string>> &ans, int n) {
// If all columns are filled, add current board to answer
if (col == n) {
ans.push_back(temp);
return;
}

for (int row = 0; row < n; row++) {
if (isSafe(row, col, temp, n)) {
// Place queen
temp[row][col] = 'Q';
// Recurse for next column
solve(col + 1, temp, ans, n);
// Backtrack and remove queen
temp[row][col] = '.';
}
}
}

// Main function to call backtracking
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> ans;
vector<string> temp;
string s(n, '.');
for(int i = 0 ; i < n ; i++) temp.push_back(s); // FIXED

solve(0, temp, ans, n);
return ans;
}

};