woshiSBSBSBSB @ 2024-07-18 11:38:55
#include<bits/stdc++.h>
using namespace std;
int row[11],n,s;
bool col[11],line1[21],line2[20];
void dfs(int k){
if(k==n+1){
for(int i=1;i<=n;i++){
if(s==3) break;
cout<<row[i]<<" ";
}
cout<<endl;
s++;
return ;
}
for(int i=1;i<=n;i++){
if(col[i]) continue;
if(line1[k+i]) continue;
if(line2[i-k+n]) continue;
row[k]=i;
col[i]=line1[k+i]=line2[i-k+n]=1;
dfs(k+1);
row[k]=0;
col[i]=line1[k+i]=line2[i-k+n]=0;
}
}
int main(){
cin>>n;
dfs(1);
cout<<s;
return 0;
}
by zhangshengzhe1 @ 2024-07-18 11:53:27
#include <bits/stdc++.h>
using namespace std;
int ntm,n;
bool a[4][50];
int ans[99];
void dfs(int i){
if(i>n){
ntm++;
if(ntm>3){
return;
}
for(int i=1;i<=n;i++)
cout<<ans[i]<<" ";
cout<<endl;
return;
}
for(int j=1;j<=n;j++){
if((!a[1][j])&&(!a[2][j+i])&&(!a[3][i-j+n])){
ans [i]=j;
a[1][j]=1;
a[2][j+i]=1;
a[3][i-j+n]=1;
dfs(i+1);
a[1][j]=0;
a[2][i+j]=0;
a[3][i-j+n]=0;
}
}
}
int main(){
cin>>n;
dfs(1);
cout<<ntm;
return 0;
}
by zhangshengzhe1 @ 2024-07-18 11:54:28
你思路有的麻烦,看看我的
by woshiSBSBSBSB @ 2024-07-18 16:45:15
栓Q 已关