Skip to content

Latest commit

 

History

History
40 lines (32 loc) · 600 Bytes

NQueue.md

File metadata and controls

40 lines (32 loc) · 600 Bytes

NQueue problem

  • 位运算实现
#include <bits/stdc++.h>
using namespace std;
 
int ans;
int lim;
 
void NQueue(int row, int ld, int rd){
    int pos, p;
    if(row ^ lim){
        pos = lim & (~(row | ld | rd));
        while(pos){
            p = pos & (~pos + 1);
            pos ^= p;
            NQueue(row | p, (ld | p) << 1, (rd | p) >> 1);
        }
    } else {
        ans++;
    }
}
 
int main()
{
    int n;
    while(scanf("%d", &n) != EOF && n){
        ans = 0;
        lim = (1 << n) - 1;
        NQueue(0, 0, 0);
        printf("%d\n", ans);
    }
    return 0;
}