
代码实现
cpp
#include <bits/stdc++.h>
using namespace std;
int n;
const int N = 15;
bool st[N], st1[2*N], st2[2*N];
vector <int> path;
int ret;
void dfs(int x)
{
if (x > n)
{
ret++;
if(ret <= 3)
{
for (auto x : path) cout << x << " ";
cout << endl;
}
return ;
}
for (int y = 1; y <= n; y++)
{
if (st[y] || st1[y-x+n] || st2[y+x] ) continue;
st[y] = st1[y-x+n] = st2[y+x] = true;
path.push_back(y);
dfs(x+1);
st[y] = st1[y-x+n] = st2[y+x] = false;
path.pop_back();
}
}
int main ()
{
cin >> n;
dfs(1);
cout << ret << endl;
return 0;
}