热门

最新

红包

立Flag

投票

同城

我的

发布
2401_88580288
2401_88580288
1 年前
true2401_88580288

#include <bits/stdc++.h>
using namespace std;
int n,tot=0,a[15];
void dfs(int step){
if(step>n) {
tot++;
if(tot<4) {
for(int i=1; i<=n; i++) {
cout<<a[i]<<' ';
}
cout<<endl;
}
return;
}
for(int i=1;i<=n;i++){
a[step]=i;//选列
int x=step,y=i,flag=1;
for(int j=1;j<step;j++){
if(j+a[j]==x+y||x-y==j-a[j]||y==a[j]){//根据数学关系排除
flag=0;
break;
}
}
if(flag) dfs(step+1);//符合选入
}
return;
}
int main(){
cin>>n;
dfs(1);
cout<<tot;
return 0;
}

CSDN App 扫码分享
分享
评论
点赞
打赏
  • 复制链接
  • 举报
下一条:
 文章入选《全站综合热榜》:解锁 DeepSeek 模型高效部署密码:蓝耘平台深度剖析与实战应用来自社区: 架构师, 频道: 今日精品佳作, https://bbs.csdn.net/topics/619568654
立即登录