// driver program to test above function int main() { solveNQ(); getchar(); return 0; }
void solveNQ(int n, int irow, vector<int> &col, int &totSol) { if(irow == n) { totSol++; return; } for(int icol = 0; icol < n; ++icol) { if(validPos(col, irow, icol)) { col.push_back(icol); solveNQ(n, irow + 1, col, totSol); col.pop_back(); } } }
int totalNQueens(int n) { vector<int> col; int totSol = 0; solveNQ(n, 0, col, totSol); return totSol; }
int main(){ solveNQ(); }