leetcode 51-60
作者:互联网
51. N 皇后
分析
与leetcode79 走迷宫方法类似
联动leetcode 79
code
class Solution {
public:
vector<vector<string>> ans;
vector<string> res;
int dg[22], udg[22], col[11];
vector<vector<string>> solveNQueens(int n) {
for (int i = 0; i < n; i ++ )
res.push_back(string(n, '.'));
for (int i = 0; i < n; i ++ )
cout << res[i] << endl;
dfs(0, n);
return ans;
}
void dfs(int u, int n){
if (u == n){
ans.push_back(res);
return ;
}
for (int i = 0; i < n; i ++ ){
if (!dg[u + i] && !udg[i - u + n] && !col[i]){
res[u][i] = 'Q';
dg[u + i] = udg[i - u + n] = col[i] = 1;
dfs(u + 1, n);
res[u][i] = '.';
dg[u + i] = udg[i - u + n] = col[i] = 0;
}
}
}
};
标签:int,dg,51,col,60,++,udg,res,leetcode 来源: https://blog.csdn.net/esjiang/article/details/114434997