Skip to main content

N-Queens

KANISHKA GUPTA
EditReport

Description:​

The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space, respectively.

Example 1:

Input: n = 4 Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]] Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.

Example 2:

Input: n = 1 Output: [["Q"]]

Video Explanation​


Approaches:​

1. Backtracking​

The most effective way to solve the N-Queens problem is using Backtracking. We place queens row by row. For each row, we try to place a queen in each column. We must ensure that the new queen is not under attack by any previously placed queens.

  1. Maintain three sets to track the columns and the two diagonals (positive and negative) that are currently occupied by queens.
  2. The positive diagonal has a constant property where row + col is the same for all elements on that diagonal.
  3. The negative diagonal has a constant property where row - col is the same for all elements on that diagonal.
  4. Create a recursive backtrack function that starts at row = 0.
  5. If row == n, a valid board configuration has been found, so we format it and add it to our results.
  6. For the current row, iterate through each column c. If placing a queen at (row, c) violates any of our sets, skip it.
  7. Otherwise, place the queen, update the sets, and recursively call backtrack(row + 1).
  8. After exploring that path, remove the queen and back out of the sets to explore other possibilities.
Warning: Values above 8 may cause performance issues.
Step 1 of 0:

Solutions Found
0
  • Time Complexity: O(N!)O(N!) where NN is the number of queens. For the first row we have NN choices, for the next roughly N−1N-1, and so on, leading to a factorial time complexity.
  • Space Complexity: O(N2)O(N^2) for storing the board state and the output array, plus O(N)O(N) for the recursion stack and the sets used to track attacks.

Solutions​

class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> res;
vector<string> board(n, string(n, '.'));
vector<int> col(n, 0), posDiag(2 * n, 0), negDiag(2 * n, 0);
backtrack(0, n, board, res, col, posDiag, negDiag);
return res;
}

private:
void backtrack(int r, int n, vector<string>& board, vector<vector<string>>& res, vector<int>& col, vector<int>& posDiag, vector<int>& negDiag) {
if (r == n) {
res.push_back(board);
return;
}
for (int c = 0; c < n; c++) {
if (col[c] || posDiag[r + c] || negDiag[r - c + n]) continue;

board[r][c] = 'Q';
col[c] = posDiag[r + c] = negDiag[r - c + n] = 1;

backtrack(r + 1, n, board, res, col, posDiag, negDiag);

board[r][c] = '.';
col[c] = posDiag[r + c] = negDiag[r - c + n] = 0;
}
}
};
Track Your Progress

Done with this topic? Mark it as complete to track your progress.

đŸ’Ŧ Discuss this page

Have a question or spot something confusing in "N-Queens"? Ask below — it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.