N-Queens II
Total Accepted: 1560 Total Submissions: 6035My SubmissionsFollow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

< http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+VGhlIGNvZGUgc2ltaWxhciB0byBJIGlzIFRMRTwvcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">class Solution { public: bool check(vector
Two ways to avoid this:
1. Use an array to record the position row[i] , col[i](true, false)
2. BIt manipulation
class Solution {
public:
unsigned int upperlimit;
void dfs(int& count, unsigned int row, unsigned int ld, unsigned int rd) {
if (row == upperlimit)
++count;
unsigned int pos = (row | ld | rd) & upperlimit; // the bit 1 simplify the position can't place the queue any more
unsigned int p = (~pos) & upperlimit,digit = 0;
while (p) {
digit = p - (p & (p - 1)); // Find the rightest 1
//digit = p&(~p + 1);
dfs(count, row + digit, (ld + digit) >> 1, (rd + digit) << 1);
p -= digit;
}
}
int totalNQueens(int n) {
upperlimit = 0;
int count = 0;
for (int i = 0; i < n; ++i)
upperlimit |= (1<