AcWing 3472. 八皇后
原题链接
中等
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e2 + 10;
const int M = 9;
int t, n, m, k, l, r, op, x, y;
string strls[N], str;
bool bo[M][M];
bool check(int y, int x) {
for (int i = 1; i < y; i++) {
if (bo[i][x])return false;
}
for (int i = y, j = x; i >= 1 && j < M; i--, j++) {
if (bo[i][j])return false;
}
for (int i = y, j = x; i >= 1 && j >= 1; i--, j--) {
if (bo[i][j])return false;
}
return true;
}
void dfs(int d) {
if (d == M) {
strls[++m] = str;
return;
}
for (int i = 1; i < M; i++) {
if (check(d, i)) {
bo[d][i] = true;
str.push_back(i + '0');
dfs(d + 1);
str.pop_back();
bo[d][i] = false;
}
}
}
void solve() {
dfs(1);
cin>>n;
for(int i = 1;i<=n;i++){
cin>>x;
cout<<strls[x]<<"\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}