- 八皇后问题
帮帮你
- @ 2026-6-6 11:51:17
#include<bits/stdc++.h>
using namespace std;
const int N=15;
int n,m,k,s,t,a[N],b[N],c[N*2],d[N*2];
int ans;
int v[110][110];
void dfs(int p){
if(p==n+1){
++ans;
cout<<"No. "<<ans<<endl;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cout<<v[j][i]<<" ";
}
cout<<endl;
}
return ;
}
for(int j=1;j<=n;j++){
if(b[j]==0 && c[p-j+n]==0 && d[p+j]==0){
a[p]=j;
b[j]=1;
c[p-j+n]=1;
d[p+j]=1;
v[p][j]=1;
dfs(p+1);
b[j]=0;
c[p-j+n]=0;
d[p+j]=0;
v[p][j]=0;
}
}
}
int main(){
n=8;
dfs(1);
return 0;
}
0 条评论
目前还没有评论...
信息
- ID
- 215
- 时间
- ms
- 内存
- MiB
- 难度
- 3
- 标签
- 递交数
- 175
- 已通过
- 46
- 上传者