八皇后 优化

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<string>
 7 #include<cmath>
 8 #include<set>
 9 #include<vector>
10 #include<stack>
11 #include<queue>
12 #include<map>
13 using namespace std;
14 #define ll long long
15 #define se second
16 #define fi first
17 const int INF= 0x3f3f3f3f;
18 const int N=5e5+5;
19 
20 int n,a[200],x1[200],x2[200],y[200],num=0; //x1,x2存的是对角线的情况
21 
22 void dfs(int x)
23 {
24     if(x>n){
25         if(num<3){
26             for(int i=1;i<=n;i++)
27                 cout<<a[i]<<' ';
28             cout<<endl;
29         }
30         num++;
31         return;
32     }
33     for(int i=1;i<=n;i++)
34     {
35         a[x]=i;
36         if( !y[i] && !x1[x+i] && !x2[x-i+n])
37         {
38             y[i]=1;
39             x1[x+i]=1;
40             x2[x-i+n]=1;
41             dfs(x+1);
42             y[i]=0;
43             x1[x+i]=0;
44             x2[x-i+n]=0;
45         }
46     }
47 }
48 
49 int main()
50 {
51     cin>>n;
52     dfs(1);
53     cout<<num;
54 }

 

转载于:https://www.cnblogs.com/thunder-110/p/9297614.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值