const int N = 20;
class Solution {
public:
vector<vector<string>> res;
bool col[N], dg[N], udg[N];
void dfs(int u, int n, vector<string>& path) {
if (u == n) {
res.push_back(path);
return;
}
for (int i = 0; i < n; i ++) {
if (!col[i] && !dg[u + i] && !udg[n - u + i]) {
path[u][i] = 'Q';
col[i] = dg[u + i] = udg[n - u + i] = true;
dfs(u + 1, n, path);
path[u][i] = '.';
col[i] = dg[u + i] = udg[n - u + i] = false;
}
}
}
vector<vector<string>> solveNQueens(int n) {
res.clear();
vector<string> path(n, string(n, '.')); // 初始化**
dfs(0, n, path);
return res;
}
};
标签:int,res,51,vector,udg,dg,path,皇后,LeetCode
From: https://www.cnblogs.com/hjy94wo/p/16650097.html