52. N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
public class Solution {
int count=0;
public int totalNQueens(int n){
solveNQueens(0, 0, n, new int[n]);
return count;
}
private void solveNQueens(int row, int col, int n, int[] column){
if (row == n)
count++;
else{
for (int i = col; i < n; i++) {
column[row]= i;
boolean f= true;
for (int r = row-1, lc= i-1, rc= i+1; r>=0 ; r--,lc--,rc++ ) {
if (column[r]== i || column[r]==lc || column[r]==rc) f= false;
}
if (f) solveNQueens(row+1, 0, n, column);
}
}
}
}