Whl0924 @ 2025-01-09 09:52:36
#include<bits/stdc++.h>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
int a[100],b[100],c[100],d[100];
int tot;
int n;
int print()
{
if(tot<=2)
{
for(int k=1;k<=n;k++)
cout<<a[k]<<" ";
cout<<endl;
}
tot++;
}
void queen(int i)
{
if(i>n)
{
print();
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;
queen(i+1);
b[j]=0;
c[i+j]=0;
d[i-j+n]=0;
}
}
}
}
int main()
{
cin>>n;
queen(1);
cout<<tot;
return 0;
}
by qfpjm @ 2025-01-09 10:22:34
%%%
by zqrmon @ 2025-01-09 10:42:52
@Whl0924 把O2优化关了就AC了
by Whl0924 @ 2025-01-10 10:23:33
@zqrmon谢谢