'commit'
This commit is contained in:
37
TangDou/AcWing/Dfs/843.cpp
Normal file
37
TangDou/AcWing/Dfs/843.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
const int N = 20;
|
||||
int path[N];
|
||||
int n;
|
||||
int b1[N], b2[N], b3[N];
|
||||
|
||||
void dfs(int u) {
|
||||
if (u == n + 1) { // 全部行都摆上皇后
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
if (j == path[i])
|
||||
printf("Q");
|
||||
else
|
||||
printf(".");
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
puts("");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) { // x行y列
|
||||
if (!b1[i] && !b2[u + i] && !b3[u - i + 8]) {
|
||||
path[u] = i;
|
||||
b1[i] = b2[u + i] = b3[u - i + 8] = 1;
|
||||
dfs(u + 1);
|
||||
b1[i] = b2[u + i] = b3[u - i + 8] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
cin >> n;
|
||||
dfs(1);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user