Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
private int[] col4Row; private int total; public int totalNQueens(int n) { col4Row = new int[n]; total = 0; placeQueen(0, n); return total; } public void placeQueen(int row, int n) { if(row == n) { total++; return; } for(int i=0; i<n; i++) { col4Row[row] = i; if(check(row)) { placeQueen(row+1, n); } } } public boolean check(int row) { for(int i=0; i<row; i++) { int diff = Math.abs(col4Row[i] - col4Row[row]); if(diff == 0 || diff == row - i) return false; } return true; }