P1562 还是N皇后

P1562 还是N皇后
原来,就会位运算搞八皇后,当复习一下。
可行的位置是1,其他是0
比如11011
到下一行的时候,对应的左斜线和右斜线要移一位

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<cstring>
 8 #define inf 2147483647
 9 #define For(i,a,b) for(register int i=a;i<=b;i++)
10 #define p(a) putchar(a)
11 #define g() getchar()
12 //by war
13 //2017.10.19
14 using namespace std;
15 int n;
16 char a[100][100];
17 int sum;
18 int tot;
19 int lim;
20 int t[1000];
21 void in(int &x)
22 {
23     int y=1;
24     char c=g();x=0;
25     while(c<'0'||c>'9')
26     {
27     if(c=='-')
28     y=-1;
29     c=g();
30     }
31     while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
32     x*=y;
33 }
34 void o(int x)
35 {
36     if(x<0)
37     {
38         p('-');
39         x=-x;
40     }
41     if(x>9)o(x/10);
42     p(x%10+'0');
43 }
44 
45 void test(int row,int left,int right,int c)
46 {
47     int pos,p=0;
48     if(row!=lim)
49     {
50         pos=lim&~(row|left|right|t[c]);
51         while(pos!=0)
52         {
53             p=pos&-pos;
54             pos=pos-p;
55             test(row+p,left+p<<1,right+p>>1,c+1);    
56         }
57     }
58     else
59     sum++;
60 }
61 
62 int main()
63 {
64     in(n);
65     lim=(1<<n)-1;
66     For(i,1,n)
67       For(j,1,n)
68         cin>>a[i][j];
69     For(i,1,n)
70       for(int j=n;j>=1;j--)
71         if(a[i][j]=='.')
72            t[i]+=(1<<(n-j));
73     test(0,0,0,1);
74     o(sum);
75      return 0;
76 }
原文地址:https://www.cnblogs.com/war1111/p/7700319.html