LeetCode:51. N 皇后
题解分析
代码
class Solution {
public:
int n;
vector<vector<string>> ans;
vector<string> path;
vector<bool> col, dg,udg;
vector<vector<string>> solveNQueens(int _n) {
n = _n;
col = vector<bool> (n);
dg = udg = vector<bool> (2 * n);
path = vector<string> (n, string(n,'.'));
dfs(0);
return ans;
}
void dfs(int y){
if(y == n){
ans.push_back(path);
return;
}
for(int x = 0; x < n; x++){
if(!col[x] && !dg[y - x + n] && !udg[y + x]){
col[x] = dg[y - x + n] = udg[y + x] = true;
path[y][x] = 'Q';
dfs(y + 1);
path[y][x] = '.';
col[x] = dg[y - x + n] = udg[y + x] = false;
}
}
}
};
原文地址:https://blog.csdn.net/qq_48290779/article/details/138199615
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!