if you come the the realization that you only need to quickly generate permutations
and the fact that you can use bitmask and bitscan, you can generalize to even much bigger boards,
like I just got 14'772'512 positions for 16 queens on a 16x16 board,
then you can realize you can also track diagonal masks incrementally
this can be simplifier further: you don't even need bsf/ctz if you only need the count
Code: Select all
constexpr int N = 16;
int perm[N];
ulong cnt = 0;
void dump_perm()
{
++cnt;
/* for (int i : N)
"%d ", 1+perm[i];
"\n";*/
}
void gen_perm(ulong mask, ulong diag1, ulong diag2, int n)
{
auto tmp = mask & ~(diag1 | diag2);
while (tmp)
{
auto bit = bsf(tmp);
tmp &= tmp-1;
perm[n] = bit;
auto zmask = 1ul << bit;
auto nmask = mask & ~zmask;
auto d1 = diag1 | zmask;
auto d2 = diag2 | zmask;
if (nmask)
gen_perm(nmask, d1 << 1, d2 >> 1, n+1);
else
dump_perm();
}
}
void main()
{
ulong mask = (1ul << N)-1;
gen_perm(mask, 0, 0, 0);
"count: %t\n", cnt;
}