提交时间:2023-11-01 13:32:42

运行 ID: 108225

#include<bits/stdc++.h> using namespace std; int a[100],k,n; bool b[100],c[100],d[100]; void print() { if(k<3) for(int x=1;x<=n;x++) cout<<a[x]<<" "; k++; } 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<<k; return 0; }