Leetcode N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
class Solution { public: int totalNQueens(int n) { vector<int> queen(n,0); for(int i = 0 ; i < n; ++ i) queen[i] = i; int cnt = 0; do{ bool flag = false; for(int i = 1; i < n && !flag; ++ i){ for(int j = 0 ; j < i && !flag ; ++ j){ if(abs(i-j) == abs(queen[i]-queen[j])) flag=true; } } if(!flag) cnt++; }while(next_permutation(queen.begin(),queen.end())); return cnt; } };