AcWing 843. n-皇后问题
原题链接
中等
作者:
洛明
,
2021-04-15 17:13:33
,
所有人可见
,
阅读 242
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10;
char cube[N][N];
int n;
bool line[N], dg[N], udg[N];
void dfs(int x){
if (x == n) {
for (int i = 0; i < n; i ++ ){
for (int j = 0; j < n; j ++ ){
if (cube[i][j] == 'Q')
cout << 'Q';
else
cout << '.';
}
cout << endl;
}
cout << endl;
return;
}
for (int i = 0; i < n; i++){
if (!line[i] && !dg[x + i] && !udg[n - x + i]){
cube[x][i] = 'Q';
line[i] = dg[x + i] = udg[n - x + i] = true;
dfs(x + 1);
line[i] = dg[x + i] = udg[n - x + i] = false;
cube[x][i] = '.';
}
}
}
int main()
{
cin >> n;
dfs(0);
}