class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<int> col(n), on_path(n), diag1(2*n), diag2(2*n);
vector<vector<string>> ans;
function<void(int)> dfs = [&](int r) {
if(r == n) {
vector<string> res(n);
for(int i = 0; i < n; i++) {
res[i] = string(col[i], '.') + 'Q' + string(n - 1 - col[i], '.');
}
ans.emplace_back(res);
return;
}
for(int c = 0; c < n; c++) {
int rc = r - c + n - 1;
if(!on_path[c] && !diag1[r + c] && !diag2[rc]) {
col[r] = c;
on_path[c] = diag1[r + c] = diag2[rc] = 1;
dfs(r + 1);
on_path[c] = diag1[r + c] = diag2[rc] = 0;
}
}
};
dfs(0);
return ans;
}
};
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!