题目链接
#include<bits/stdc++.h>using namespace std;const int N = 40;
int a[N],b[N],c[N],d[N];
int n,flag;void dfs(int i)
{
if(i>n){
if(flag<=2){
for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;}flag++;return ;}else{
for(int j=1;j<=n;j++){
if(!b[j]&&!c[i+j]&&!d[i-j+n]){
a[i] = j;b[j] = 1;c[i+j] = 1;d[i-j+n] = 1;dfs(i+1);b[j] = 0;c[i+j] = 0;d[i-j+n] = 0;}}}
}int main()
{
cin>>n;dfs(1);cout<<flag<<endl;return 0;
}