longlong20120308 @ 2024-11-01 14:48:01
代码
#include <bits/stdc++.h>
using namespace std;
#define maxn 100
long long a[maxn], n, ans = 0;
long long b1[maxn], b2[maxn], b3[maxn];
void dfs(long long x) {
if (x > n) {
ans++;
if (ans <= 3) {
for (int i = 1; i <= n; i++) {
cout << a[i];
puts("");
}
return;
}
for (int i = 1; i <= n; i++) {
if (b1[i] == 0 && b2[x + 1] == 0 && b3[x - i + 15] == 0) {
a[x] = i;
b1[i] = 1;
b2[x + i] = 1;
b3[x - i + 15] = 1;
dfs(x + 1);
b1[i] = 0;
b2[x + i] = 0;
b3[x - i + 15] = 0;
}
}
}
}
int main() {
cin >> n;
dfs(1);
cout << ans;
return 0;
}
by Ff472130 @ 2024-11-01 15:00:07
@longlong20120308
#include <bits/stdc++.h>
using namespace std;
#define maxn 100
long long a[maxn], n, ans = 0;
long long b1[maxn], b2[maxn], b3[maxn];
void dfs(long long x) {
if (x > n) {
ans++;
if (ans <= 3) {
for (int i = 1; i <= n; i++) {
cout << a[i] << ' ';
}
puts("");
return;
}
}
for (int i = 1; i <= n; i++) {
if (b1[i] == 0 && b2[x + i] == 0 && b3[x - i + 15] == 0) {
a[x] = i;
b1[i] = 1;
b2[x + i] = 1;
b3[x - i + 15] = 1;
dfs(x + 1);
b1[i] = 0;
b2[x + i] = 0;
b3[x - i + 15] = 0;
}
}
}
int main() {
cin >> n;
dfs(1);
cout << ans;
return 0;
}
by longlong20120308 @ 2024-11-01 15:18:07
@Ff472130 谢谢,已关