Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
class Solution { public: bool isOK(vector<int> &vec, int index) { int size = vec.size(); if(0 == size) return true; for(int i=0;i<size;i++) { if(vec[i] == index) return false; if(abs(index - vec[i])==size-i) return false; } return true; } void solve(int &num, vector<int> &vec, int n) { if(vec.size() == n) { num++; return; } for(int i=0; i<n;i++) { if(!isOK(vec,i)) continue; vec.push_back(i); solve(num, vec, n); vec.pop_back(); } } int totalNQueens(int n) { int num = 0; vector<int> vec; solve(num, vec, n); return num; } };