lucy2012 @ 2024-05-02 21:19:12
这题我做第三次,次次有不明白的地方,这次:
附赠代码:
#include<bits/stdc++.h>
using namespace std;
int n,sum=0,d1[110],d2[110],d3[30],a[30];
void dfs(int x){
if(x>n){
sum++;
if(sum<=3){
for(int i=1;i<=n;i++)
cout<<a[i]<<' ';
cout<<endl;
}
return;
}
for(int i=1;i<=n;i++){
if(d3[i]==0&&d1[x+i]==0&&d2[x-i+15]==0){
a[x]=i;
d3[i]=1;d1[x+i]=1;d2[x-i+15]=1;
dfs(x+1);
d3[i]=0;d1[x+i]=0;d2[x-i+15]=0;
}
}
}
int main() {
cin>>n;
dfs(1);
cout<<sum;
return 0;
}
by I_Love_DS @ 2024-05-02 21:46:42
脑子里只有白色
by smile_jyc @ 2024-05-02 21:47:39
@lucy2012 枚举子集
#include<bits/stdc++.h>
using namespace std;
long long rol[1000],col[1000],u[1000],v[1000];//行列
long long n,cnt=0;
long long dfs(long long cur){
if(cur>n){
if(cnt<=2){
for(int i=1;i<=n;i++){
cout<<rol[i]<<" ";;
}
cout<<endl;
}
cnt++;
return 0;
}
for(long long i=1;i<=n;i++){
if((!col[i])&&(!u[cur+i])&&(!v[cur-i+n])){
rol[cur]=i;
col[i]=1;
u[cur+i]=1;
v[cur-i+n]=1;
dfs(cur+1);
col[i]=0;
u[cur+i]=0;
v[cur-i+n]=0;
}
}
}
int main(){
cin>>n;
dfs(1);
cout<<cnt;
return 0;
}
by lucy2012 @ 2024-05-02 21:55:51
@hhhcj @liuruiqing @smile_jyc 告诉大家一个好消息,我是正常人,终于懂了!!!(皆大欢喜
by lucy2012 @ 2024-05-02 21:57:21
开心
by I_Love_DS @ 2024-05-03 16:38:41
亦开心也!
by lucy2012 @ 2024-05-03 16:55:25
理解完就写了篇题解