题目要求:
n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。
每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
示例 1:
输入:n = 4
输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
解释:如上图所示,4 皇后问题存在两个不同的解法。
示例 2:输入:n = 1
输出:[["Q"]]
提示:1 <= n <= 9
皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/n-queens
解题代码:
class Solution {
private:
vector<vector<string>> res;
vector<bool> col, dia1, dia2;
// 尝试在一个n皇后问题中,拜访第index行的皇后位置
void putQueen(int n, int index, vector<int> &row) {
if (index == n) {
res.push_back(generateBoard(n, row));
return;
}
for (int i = 0; i < n; ++i) {
// 尝试将第index行的皇后拜访在第i列
if (!col[i] && !dia1[index + i] && !dia2[index - i + n - 1]) {
row.push_back(i);
col[i] = true;
dia1[index + i] = true;
dia2[index - i + n - 1] = true;
putQueen(n, index + 1, row);
// 回溯
col[i] = false;
dia1[index + i] = false;
dia2[index - i + n - 1] = false;
row.pop_back();
}
}
return;
}
vector<string> generateBoard(int n, vector<int>& row) {
assert(row.size() == n);
vector<string> board(n, string(n, '.'));
for (int i = 0; i < n; ++i) {
board[i][row[i]] = 'Q';
}
return board;
}
public:
vector<vector<string>> solveNQueens(int n) {
res.clear();
col = vector<bool>(n, false);
dia1 = vector<bool>(2 * n - 1, false);
dia2 = vector<bool>(2 * n - 1, false);
vector<int> row;
putQueen(n, 0, row);
return res;
}
};
class Solution {
private:
vector<vector<string>> res;
vector<bool> col, dia1, dia2;
// 尝试在一个n皇后问题中,拜访第index行的皇后位置
void putQueen(int n, int index, vector<int> &row) {
if (index == n) {
res.push_back(generateBoard(n, row));
return;
}
for (int i = 0; i < n; ++i) {
// 尝试将第index行的皇后拜访在第i列
if (!col[i] && !dia1[index + i] && !dia2[index - i + n - 1]) {
row.push_back(i);
col[i] = true;
dia1[index + i] = true;
dia2[index - i + n - 1] = true;
putQueen(n, index + 1, row);
// 回溯
col[i] = false;
dia1[index + i] = false;
dia2[index - i + n - 1] = false;
row.pop_back();
}
}
return;
}
vector<string> generateBoard(int n, vector<int>& row) {
assert(row.size() == n);
vector<string> board(n, string(n, '.'));
for (int i = 0; i < n; ++i) {
board[i][row[i]] = 'Q';
}
return board;
}
public:
vector<vector<string>> solveNQueens(int n) {
res.clear();
col = vector<bool>(n, false);
dia1 = vector<bool>(2 * n - 1, false);
dia2 = vector<bool>(2 * n - 1, false);
vector<int> row;
putQueen(n, 0, row);
return res;
}
};
class Solution { private: vector<vector<string>> res; vector<bool> col, dia1, dia2; // 尝试在一个n皇后问题中,拜访第index行的皇后位置 void putQueen(int n, int index, vector<int> &row) { if (index == n) { res.push_back(generateBoard(n, row)); return; } for (int i = 0; i < n; ++i) { // 尝试将第index行的皇后拜访在第i列 if (!col[i] && !dia1[index + i] && !dia2[index - i + n - 1]) { row.push_back(i); col[i] = true; dia1[index + i] = true; dia2[index - i + n - 1] = true; putQueen(n, index + 1, row); // 回溯 col[i] = false; dia1[index + i] = false; dia2[index - i + n - 1] = false; row.pop_back(); } } return; } vector<string> generateBoard(int n, vector<int>& row) { assert(row.size() == n); vector<string> board(n, string(n, '.')); for (int i = 0; i < n; ++i) { board[i][row[i]] = 'Q'; } return board; } public: vector<vector<string>> solveNQueens(int n) { res.clear(); col = vector<bool>(n, false); dia1 = vector<bool>(2 * n - 1, false); dia2 = vector<bool>(2 * n - 1, false); vector<int> row; putQueen(n, 0, row); return res; } };