LYL__1107 @ 2024-10-04 16:45:41
#include<bits/stdc++.h>
using namespace std;
int sum,n,h[200],l[200],zd[200],yd[200];
int print(){
if(sum<=2){
for(int j=1;j<=n;j++) cout<<h[j]<<" ";
cout<<endl;
}
sum++;
}
void dfs(int i){
if(i>n){
print();
return;
}else{
for(int j=1;j<=n;j++){
if(l[j]==0&&yd[i+j]==0&&zd[i-j+n]==0){
h[i]=j;
l[j]=1;
yd[i+j]=1;
zd[i-j+n]=1;
dfs(i+1);
l[j]=0;
yd[i+j]=0;
zd[i-j+n]=0;
}
}
}
}
int main(){
cin>>n;
dfs(1);
cout<<sum;
}
结果
by sieve @ 2024-10-04 16:53:58
int print(){
if(sum<=2){
for(int j=1;j<=n;j++) cout<<h[j]<<" ";
cout<<endl;
}
sum++;
}
这里,要设为void类型,如果是int的话,就会RE
by sea_bird @ 2024-10-12 13:30:07
print函数无返回值
by LYL__1107 @ 2024-12-15 08:24:18
好的,谢谢