This question is boring, and the number of solutions is actually output. The previous question outputs all solutions. Do you still have to know the number of solutions ..
I suspect that the number of solutions here is something similar to a general item. I checked it online. No, I finally changed the code of the previous question a bit.
class Solution {public: int totalNQueens(int n) { if(n == 0) return 0; int pos[n], res=0, start=0; bool flag, vis[n]; memset(pos, 0, sizeof(pos)); memset(vis, 0, sizeof(vis)); int i=0, j; while(i<n){ for(j=start;j<n;j++){ if(vis[j]) continue; flag = true; for(int k=0;k<i;k++){ if(abs(k-i) == abs(pos[k]-j)){ flag = false; break; } } if(!flag) continue; vis[j] = 1; pos[i] = j; break; } if(j == n){ --i; vis[pos[i]] = 0; start = pos[i]+1; if(i==0&&start>=n) break; continue; }else{ i++; start = 0; } if(i == n){ res++; --i; vis[pos[i]] = 0; start = pos[i]+1; if(i==0&&start>=n) break; } } return res; }};