class Solution:
def solveNQueens(self, n: int):
def backtrack(row, cols, diagonals1, diagonals2, board, res):
if row == n:
res.append([''.join(r) for r in board])
return
for col in range(n):
if col in cols or (row - col) in diagonals1 or (row + col) in diagonals2:
continue
cols.add(col)
diagonals1.add(row - col)
diagonals2.add(row + col)
board[row][col] = 'Q'
backtrack(row + 1, cols, diagonals1, diagonals2, board, res)
board[row][col] = '.'
cols.remove(col)
diagonals1.remove(row - col)
diagonals2.remove(row + col)
res = []
board = [['.' for _ in range(n)] for _ in range(n)]
backtrack(0, set(), set(), set(), board, res)
return res
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> res;
vector<string> board(n, string(n, '.'));
unordered_set<int> cols, diag1, diag2;
backtrack(0, n, cols, diag1, diag2, board, res);
return res;
}
void backtrack(int row, int n, unordered_set<int>& cols, unordered_set<int>& diag1, unordered_set<int>& diag2,
vector<string>& board, vector<vector<string>>& res) {
if (row == n) {
res.push_back(board);
return;
}
for (int col = 0; col < n; ++col) {
if (cols.count(col) || diag1.count(row - col) || diag2.count(row + col)) continue;
cols.insert(col);
diag1.insert(row - col);
diag2.insert(row + col);
board[row][col] = 'Q';
backtrack(row + 1, n, cols, diag1, diag2, board, res);
board[row][col] = '.';
cols.erase(col);
diag1.erase(row - col);
diag2.erase(row + col);
}
}
};
class Solution {
public List<List<String>> solveNQueens(int n) {
List<List<String>> res = new ArrayList<>();
char[][] board = new char[n][n];
for (char[] row : board) Arrays.fill(row, '.');
backtrack(0, n, new HashSet<>(), new HashSet<>(), new HashSet<>(), board, res);
return res;
}
private void backtrack(int row, int n, Set<Integer> cols, Set<Integer> diag1, Set<Integer> diag2,
char[][] board, List<List<String>> res) {
if (row == n) {
List<String> solution = new ArrayList<>();
for (char[] r : board) solution.add(new String(r));
res.add(solution);
return;
}
for (int col = 0; col < n; ++col) {
if (cols.contains(col) || diag1.contains(row - col) || diag2.contains(row + col)) continue;
cols.add(col);
diag1.add(row - col);
diag2.add(row + col);
board[row][col] = 'Q';
backtrack(row + 1, n, cols, diag1, diag2, board, res);
board[row][col] = '.';
cols.remove(col);
diag1.remove(row - col);
diag2.remove(row + col);
}
}
}
var solveNQueens = function(n) {
const res = [];
const board = Array.from({length: n}, () => Array(n).fill('.'));
function backtrack(row, cols, diag1, diag2) {
if (row === n) {
res.push(board.map(r => r.join('')));
return;
}
for (let col = 0; col < n; ++col) {
if (cols.has(col) || diag1.has(row - col) || diag2.has(row + col)) continue;
cols.add(col);
diag1.add(row - col);
diag2.add(row + col);
board[row][col] = 'Q';
backtrack(row + 1, cols, diag1, diag2);
board[row][col] = '.';
cols.delete(col);
diag1.delete(row - col);
diag2.delete(row + col);
}
}
backtrack(0, new Set(), new Set(), new Set());
return res;
};
n
queens on an n x n
chessboard so that no two queens threaten each other. In chess, a queen can attack any other piece in the same row, column, or diagonal. Your task is to return all distinct solutions to the n-queens puzzle, where each solution is represented as a list of strings. Each string represents a row, with 'Q'
indicating a queen and '.'
indicating an empty space. The goal is to find all valid configurations, not just one. Each queen must occupy a unique row and column, and no two queens can share a diagonal.
n
rows and n
columns, the number of possible arrangements grows very quickly (exponentially). Placing queens randomly and checking for conflicts is inefficient and impractical.
To make the problem manageable, we can use backtracking. This means we build the solution row by row, placing a queen in each row only if it doesn't conflict with any previously placed queens. If we get stuck (no valid columns in a row), we back up and try a different position for the last queen. By keeping track of which columns and diagonals are already occupied, we avoid unnecessary checks and speed up our search.
n = 4
:
. Q . . . . . Q Q . . . . . Q .
n^n
possible ways to place queens, since each of the n
rows could have a queen in any of n
columns. This is extremely inefficient.n^n
. The time complexity is O(n!), since for each row, we have fewer and fewer valid columns to choose from as we go deeper.