https://leetcode.com/problems/n-queens-ii/
Follow up for n-queens problem.
Now, instead outputting board configurations and return the total number of distinct solutions.
Problem Solving Ideas:
What's the simplest way to compare this with N-queens? No, nothing more than to figure out the size of all the results returned after the solution. Of course, you can also maintain a value, increment the result, and then return to him.
Public classSolution { Public intTotalnqueens (intN) {List<String[]> result =NewArraylist<string[]>(); if(N < 1) { return0; } string[] current=NewString[n]; for(inti = 0; I < n; i++) {StringBuffer bf=NewStringBuffer (); for(intj = 0; J < N; J + +) {bf.append (‘.‘); } Current[i]=bf.tostring (); } int[] Columninrow =New int[n]; for(inti = 0; I < n; i++) {Columninrow[i]=-1; } DFS (n, result, current, Columninrow,0); returnresult.size (); } Public voidDfsintN, list<string[]> result, string[] current,int[] Columninrow,introw) { if(Row = =N) {string[] temp=arrays.copyof (current, current.length); Result.add (temp); return; } for(inti = 0; I < n; i++) { if(Checkvalid (Columninrow, Row, i)) {Columninrow[row]=i; String Temp=Current[row]; Current[row]= current[row].substring (0, I) + "Q" + current[row].substring (i + 1)); DFS (n, result, current, columninrow, row+ 1); Current[row]=temp; Columninrow[row]=-1; } } } Public BooleanCheckvalid (int[] Columninrow,intRowintcolumn) { inttemp = row-1, i = 1; while(Temp >= 0) { if(Columninrow[temp] = =column) { return false; } if(column-i >= 0 && columninrow[temp] = = Column-i) {return false; } if(column + i < columninrow.length && columninrow[temp] = = column +i) {return false; } I++; Temp--; } temp= row + 1; I= 1; while(Temp <columninrow.length) {if(Columninrow[temp] = =column) { return false; } if(column-i >= 0 && columninrow[temp] = = Column-i) {return false; } if(column + i < columninrow.length && columninrow[temp] = = column +i) {return false; } I++; Temp++; } return true; }}
It also optimizes the process of constructing a string, and returns the size directly.
Public classSolution { Public intTotalnqueens (intN) {if(N < 1) { return0; } int[] Columninrow =New int[n]; for(inti = 0; I < n; i++) {Columninrow[i]=-1; } intresult = 0; Result= DFS (n, columninrow, 0, result); returnresult; } Public intDfsintNint[] Columninrow,intRowintresult) { if(Row = =N) {result++; returnresult; } for(inti = 0; I < n; i++) { if(Checkvalid (Columninrow, Row, i)) {Columninrow[row]=i; Result= DFS (n, columninrow, row + 1, result); Columninrow[row]=-1; } } returnresult; } Public BooleanCheckvalid (int[] Columninrow,intRowintcolumn) { inttemp = row-1, i = 1; while(Temp >= 0) { if(Columninrow[temp] = =column) { return false; } if(column-i >= 0 && columninrow[temp] = = Column-i) {return false; } if(column + i < columninrow.length && columninrow[temp] = = column +i) {return false; } I++; Temp--; } temp= row + 1; I= 1; while(Temp <columninrow.length) {if(Columninrow[temp] = =column) { return false; } if(column-i >= 0 && columninrow[temp] = = Column-i) {return false; } if(column + i < columninrow.length && columninrow[temp] = = column +i) {return false; } I++; Temp++; } return true; }}
N-queens II