提交时间:2023-10-14 10:55:00
运行 ID: 106723
#include<bits/stdc++.h> #define int long long using namespace std; const int N=100005; bool d[N],b[N],c[N]; int a[N]; int n; int dfs(int i){ int j; for(j=1;j<=n;j++){ if((!b[j])&&(!c[i+j])&&(!d[i-j+(n-1)])){ a[i]=j; b[j]=1; c[i+j]=1; d[i-j+(n-1)]=1; if(i==n){ for(int s=1;s<=n;s++){ cout<<a[s]<<' '; } cout<<endl; } else{ dfs(i+1); } b[j]=0; c[i+j]=0; d[i-j+(n-1)]=0; } } } signed main(){ cin>>n; dfs(1); return 0; }