problem:
Follow up for n-queens problem.
Now, instead outputting board configurations and return the total number of distinct solutions.
Link
https://leetcode.com/problems/n-queens-ii/
Analysis:
Prolem Definition:https://En.wikipedia.org/wiki/eight_queens_puzzle--------------------------------------------------------------------------------------Solution1: (Wrong solution, No. Queens could is placed on the same diagonal line.) Public intTotalnqueens (intN) {if(n <= 0) return0; ArrayList<Integer> ret =NewArraylist<integer> (); Ret.add (0); Boolean[] Used_column =New Boolean[n]; for(intj = 0; J < N; J + +) {Helper (Used_column, N,0, J, ret); } returnRet.get (0); } Private voidHelperBoolean[] Used_column,intNintIintJ, Arraylist<integer>ret) { if(i = = n-1 && Used_column[j] = =false) {Ret.set (0, Ret.get (0) + 1); return; } if(Used_column[j]) {return; } Else{Used_column[j]=true; for(intNext_column = 0; Next_column < n; Next_column + +) {Helper (Used_column, n, I+ 1, Next_column, ret); } Used_column[j]=false; }} at the first glance, it seems we have the use a and the dimensional array to the record each queen' s position, since we need to check there is, Queens on the same diagonal. However, there is a very important skill in solving the Problem:each row would only taken up a single queen. Here, we introduce a more elegant and powerful solution for Thisproblem. Basic idea:we Check eachNewPlacement against the post placement, it is valid weContinueOn the next row. (the same idea as above solution). However could significantly alleviate the coding work, by use inherently backtracking. At above solution, helper (Boolean[] Used_column,intNintIintJ, Arraylist<integer>ret)------------------------------------------------------------------------We specify the column number J forThe row i. The Incovenience for Thismethod is, we need obviously recover the used_column ' s state before we specify the column number j+1.This design introduce the following code snippt.------------------------------------------------------------Used_column[j]=true; for(intNext_column = 0; Next_column < n; Next_column + +) {Helper (Used_column, n, I+ 1, Next_column, ret);} USED_COLUMN[J]=false; ( forBack tracking Purpose) Also introduce following ugly code snippt when invoking helper. for(intj = 0; J < N; J + +) {Helper (Used_column, N,0, J, ret);} The above is too ugly for ThisProbelm. We could take advantage of "For-loop"Over the same element, so as to avoid obviously backtracking.helper (intRowintNint[] Column_for_row, arraylist<integer>ret) At all helper, we test against each column placement fora row. for(inti = 0; I < n; i++) {Column_for_row[row]=i; if(IsValid (row, Column_for_row)) helper (Row+ 1, N, Column_for_row, ret);} Even the current placement are not valid, the forLoop could easily take us forThe next column placement by overwriting the Column_for_row[row] into i+1. Skills:1. Use the Exceed CaseAs base Case. Since at each helper, we firstly checkifThe current placement are valid, and then go to the next row. If we reach the row n, it means the rows before n-1is all valid.if(Row = =N) {ret.set (0, Ret.get (0) + 1); return;}2. Use the Column_for_row to check agianst vioation cases.------------------------------------------------------------2.1Queens were placed in the same column.if(Column_for_row[i] = =Column_for_row[row])2.2Queens on the same diagonal line.if(row-i) = = Math.Abs (Column_for_row[row]-Column_for_row[i])) Note:must use ABS, not sure which placement are at the lower column. Time Complexity:even though ThisQuestion only Ask forThe total count. We still has to search along all possible routines. At each row, we could place a queen in any column. (At least we have tested forEach placement) Thus the time complexity is sadly O (n^2)
Solution:
Public classSolution { Public intTotalnqueens (intN) {if(n <= 0) return0; ArrayList<Integer> ret =NewArraylist<integer> (); Ret.add (0); int[] Column_for_row =New int[n]; Helper (0, N, Column_for_row, ret); returnRet.get (0); } Private voidHelperintRowintNint[] Column_for_row, arraylist<integer>ret) { if(Row = =N) {ret.set (0, Ret.get (0) + 1); return; } //a totally different the thinking!!!! for(inti = 0; I < n; i++) {Column_for_row[row]=i; if(IsValid (row, Column_for_row)) helper (Row+ 1, N, Column_for_row, ret); } } Private BooleanIsValid (intRowint[] column_for_row) { for(inti = 0; i < row; i++) { if(Column_for_row[i] = = Column_for_row[row] | | (row-i) = = Math.Abs (Column_for_row[row]-Column_for_row[i])) return false; } return true; }}
[LEETCODE#52] N-queens II