mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-24 18:11:59 +00:00
843. n皇后问题
https://www.acwing.com/problem/content/submission/code_detail/6188239/
This commit is contained in:
parent
15534a7857
commit
f2a72b8e2d
37
AcWing/843/843.cpp
Normal file
37
AcWing/843/843.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int n;
|
||||
bool col[20], dg[20], udg[20];
|
||||
char g[20][20];
|
||||
|
||||
void dfs(int u) {
|
||||
if (u == n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << g[i] << endl;
|
||||
}
|
||||
cout << endl;
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (!col[i] && !dg[u + i] && !udg[n - u + i]) {
|
||||
g[u][i] = 'Q';
|
||||
col[i] = dg[u + i] = udg[n - u + i] = true;
|
||||
dfs(u + 1);
|
||||
col[i] = dg[u + i] = udg[n - u + i] = false;
|
||||
g[u][i] = '.';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
for(int i = 0 ; i < n ; i++) {
|
||||
for(int j = 0 ; j < n ; j++) {
|
||||
g[i][j] = '.';
|
||||
}
|
||||
}
|
||||
dfs(0);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user