加载头像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//n皇后问题
#include<bits/stdc++.h>
using namespace std;
int a[10001];//保存皇后的位置
int b[10001],c[10001],d[10001];//标记同列和对角线
int sum;//方案总数
void print()
{
sum++;
for(int i=1;i<=8;i++)
{
cout<<a[i];
if(i!=8) cout<<" ";
else cout<<endl;
}
}
void s(int i)
{
for(int j=1;j<=8;j++)//试探八个位置
{
if(!b[j] && !c[i+j] && !d[i-j+7])//下标不为负,+7
{
a[i]=j;
b[j]=1;//j列被占用
c[i+j]=1;//对角线被占用
d[i-j+7]=1;
if(i==8) print();//放完
else s(i+1);
b[j]=0;//回溯
c[i+j]=0;
d[i-j+7]=0;
}
}
}
int main()
{
s(1);
cout<<sum<<endl;
}

pFFKw0e.jpg

image

image


avatar
status
这有关于产品、设计、开发相关的问题和看法,还有文章翻译分享
相信你可以在这里找到对你有用的知识教程
公告

Minecraft-Sep的博客已更新至v1.6.0版本!
基于CodebergVercelCloudflare的Sep博客已上线!详情见此处!
您正在浏览安全的Pages页面!

引用到评论
随便逛逛博客分类文章标签
复制地址关闭热评深色模式轉為繁體