#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<deque>
#include<set>
#include<map>
using namespace std;
const int N=15;
int row[N],lft[N],rgt[N],res,n,a[N];
void dfs(int u)
{
if(u==n+1)
{
res++;
//cout<<res<<". ";
if(res<=3)
{
for(int i=1;i<=n;++i) cout<<a[i]<<' ';
cout<<endl;
//cout<<"==========================="<<endl;
}
return;
}
for(int i=1;i<=n;++i)
{
if(row[i]||lft[u+i]||rgt[u-i+n]) continue;
a[u]=i;
row[i]=true;
lft[u+i]=true;
rgt[u-i+n]=true;
dfs(u+1);
row[i]=false;
lft[u+i]=false;
rgt[u-i+n]=false;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
dfs(1);
cout<<res<<endl;
return 0;
}
输入
11
输出
11
1 3 7 10 2 9 6 4 11 8 5
1 4 6 10 3 11 8 5 2 9 7
1 4 7 10 2 5 8 11 3 6 9
1 4 7 11 8 3 5 9 2 6 10
1 4 9 3 8 2 11 6 10 7 5
1 4 9 7 10 2 11 6 8 3 5
1 5 2 8 11 7 3 10 6 4 9
1 5 9 2 6 10 3 7 11 4 8
1 6 4 2 11 8 10 5 3 9 7
1 6 4 10 8 3 5 2 11 9 7
1 6 8 10 4 9 5 2 11 3 7
1 7 4 8 11 5 10 2 6 3 9
1 8 4 11 7 3 10 6 2 9 5
1 8 6 2 9 11 4 7 10 3 5
168
Process returned 0 (0x0) execution time : 1.273 s
Press any key to continue.