#33603: ans


yp11151230@yphs.tp.edu.tw (710-43蔡亞儒)

學校 : 臺北市私立延平高級中學
編號 : 197211
來源 : [203.72.178.1]
最後登入時間 :
2023-06-14 17:43:23
d543. 挑战极限 Part7:强大的N皇后 -- N皇后问题的终极优化 | From: [203.72.178.2] | 發表日期 : 2023-01-13 13:40

#include <bits/stdc++.h>
using namespace std;

int n, counter;

void dfs(unsigned col, unsigned diag1, unsigned diag2) {
    if (col == n) counter++;
    for (unsigned ava=n&~(col|diag1|diag2), now=ava&-ava; // now is the current position to be placed.
        ava; ava^=now, now=ava&-ava) // ava is short for available columns.
        dfs(col|now, (diag1|now)<<1, (diag2|now)>>1);
}

int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); // IO 優化
    while (cin >> n)
        counter=0, n=(1<<n)-1, dfs(0, 0, 0), cout << counter << '\n';
    return 0;
}

 
ZeroJudge Forum