jhlcxoi114514 @ 2024-03-03 11:11:08
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,tot,a[10];
void print(){
tot++;
if(tot>3) return;
for(int i=1;i<=n;i++){
cout<<a[i]<<" ";
}
cout<<"\n";
}
void dfs(int k){
bool fl;
if(k>n) print();
else{
for(int j=1;j<=n;j++){
a[k]=j;
fl=1;
for(int i=k-1;i>=1;i--){
if(a[k]==a[i]||
k-a[k]==i-a[i]||
k+a[k]==i+a[i]){
fl=0;
break;
}
}
if(fl) dfs(k+1);
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
dfs(1);
cout<<tot;
}
AC1~4,错5~8.
先看第五样例
10
1 3 6 8 10 5 9 2 4 7
1 3 6 9 7 10 4 2 5 8
1 3 6 9 7 10 4 2 8 5
724
不信自己测~~~~
by Wanzm_Alymine @ 2024-03-05 18:57:37
@jhlcxoi114514 数组开小了?
by jhlcxoi114514 @ 2024-03-06 21:35:30
@Wanzm
AC了
感谢大佬