Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
int total = 0;
int col[100];
void dfs(int cur, int n)
{
if(cur==n)
total++;
else for(int i=0; i<n; i++)
{
col[cur]=i;
bool isOk=true;
for(int j=0; j<cur; j++)
{
if(col[cur]==col[j] || cur-col[cur] == j-col[j] || cur+col[cur] == j+col[j])
{
isOk=false;
break;
}
}
if(isOk)
{
dfs(cur+1, n);
}
}
}
int totalNQueens(int n)
{
dfs(0, n);
return total;
}