N皇后

二进制log2(t)+1 便是位数

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n;
int p;
int s=0;
int sol[999];
int ans=0;
int log2(int a)//log
{
switch (a)
{
case 1:
return 1;
case 2:
return 2;
case 4:
return 3;
case 8:
return 4;
case 16:
return 5;
case 32:
return 6;
case 64:
return 7;
case 128:
return 8;
case 256:
return 9;
case 512:
return 10;
case 1024:
return 11;
case 2048:
return 12;
case 4096:
return 13;
}
}
void print()
{
for(int i=1;i<=n;i++)
{
printf("%d ",log2(sol[i]));
}
printf("\n");
}
void dfs(int r,int rl,int ll)
{
if(r==p)
{
ans++;
if(ans<=3)
print();
return;
}
int pos=p&~(r|rl|ll);
while(pos!=0)
{
int t=pos&(-pos);
pos-=t;

sol[++s]=t;
dfs(r|t,(rl|t)<<1,(ll|t)>>1);
--s;
}
}
int main()
{
scanf("%d",&n);
p=(1<<n)-1;
dfs(0,0,0);
printf("%d",ans);
return 0;
}

最近的文章

抓住那头牛

广度优先搜索,深搜超时需要判断界限,做标记,已到过的地点就不必去了12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758#include&l …

于  搜索 继续阅读
更早的文章

金明的预算方案

组合背包12345678910111213141516171819202122232425262728293031323334#include&lt;iostream&gt;using namespace std;int v[9999];int w[9999];int k[9999];int f[9 …

于  组合背包dp 继续阅读