Follow up for n-queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
Answer
Public class solution {int queenposition []; int N; int totalnum; set <integer> getremainingposition (INT index) {set <integer> result = new hashset <integer> (); int I, j; for (I = 0; I <n; I ++) {result. add (I) ;}for (I = 0; I <n; I ++) {for (j = 0; j <index; j ++) {If (I = queenposition [J]) | (math. ABS (I-queenposition [J]) = math. ABS (index-j) {result. remove (I); break ;}}return result;} public void calnqueens (INT index) {set <integer> remainingposition = getremainingposition (INDEX ); if (index + 1 = N) {totalnum + = remainingposition. size () ;}else {for (integer position: remainingposition) {queenposition [Index] = position; calnqueens (index + 1) ;}} public int totalnqueens (int n) {n = N; totalnum = 0; queenposition = new int [N]; calnqueens (0); Return totalnum ;}}
N-Queens II