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
| class Solution { public: int res; int Nqueen(int n) { res = 0; dfs(0, n, 0, 0, 0); return res; }
void dfs(int u, int n, int col, int pie, int na) { if (u == n) { res++; return; }
for (int j = 0; j < n; ++j) { if ((col & 1 << j) || (pie & (1 << (n + u - j))) || (na & (1 << (u + j)))) continue; dfs(u + 1, n, col | (1 << j), pie | (1 << (n + u - j)), na | (1 << (u + j))); } } };
|