huino @ 2023-11-29 21:26:28
#include <iostream>
using namespace std;
int n,cnt = 0;
int h1[20] = {0},x1[40] = {0},x2[40] = {0};
void dep(int dp,int l)
{
if(dp == n+1)
{
cnt++;
for(int i=1;i<=n;i++)
{
cout<<h1[i]<<" ";
}
cout<<endl;
return;
}
if(l <= 0||l > n)
return;
for(int h=1;h<=n;h++)
{
if(h1[h] == 0&&!x1[l+h]&&!x2[n + l - h])
{
h1[h] = l;
x1[l+h] = 1;
x2[n + l - h] = 1;
dep(dp+1,l+1);
h1[h] = 0;
x1[l+h] = 0;
x2[n + l - h] = 0;
}
}
}
int main()
{
cin>>n;
dep(1,1);
cout<<cnt;
return 0;
}
by Ryzen_9_7950X @ 2023-12-03 20:56:47
可以将每次运算的结果存起来,然后排序即可
by huino @ 2024-02-17 19:08:11
@Ryzen_9_7950X 是个方法,但是这道题列举的数目太多了,后期打表可以,直接提交就算不加筛选也会超时。