提交时间:2023-11-02 13:16:56

运行 ID: 108334

#include<bits/stdc++.h> using namespace std; int a[100],k,n; bool b[100],c[100],d[100]; void print() { k++; if(k<=3) for(int x=1;x<=n;x++) cout<<a[x]<<" "; if(k<3) cout<<endl; return; } void dfs(int i) { if(i>n) { print(); return; } else { for(int j=1;j<=n;j++) { if(b[j]==0&&c[i-j+n]==0&&d[i+j]==0) { a[i]=j; b[j]=1; c[i-j+n]=1; d[i+j]=1; dfs(i+1); b[j]=0; c[i-j+n]=0; d[i+j]=0; } } } } int main() { cin>>n; dfs(1); cout<<endl<<k; return 0; }