@@ -0,0 +1,2024 @@
+public class Solution %7B%0A    public int totalNQueens(int n) %7B%0A        ret = new HashSet%3CList%3CString%3E%3E();%0A        char%5B%5D%5B%5D puzzle = new char%5Bn%5D%5Bn%5D;%0A        for(int i=0; i%3Cn; i++) for(int j=0; j%3Cn; j++) puzzle%5Bi%5D%5Bj%5D = '.';%0A        backtrack(puzzle, 0, n);%0A        return ret.size();%0A    %7D%0A    %0A    private Set%3CList%3CString%3E%3E ret;%0A    %0A    private void backtrack(char%5B%5D%5B%5D puzzle, int k, int n) %7B%0A        if (is_a_solution(k, n)) %7B%0A            ret.add(process_solution(puzzle, n));%0A            return;%0A        %7D%0A        %0A        String candidate = constructCandidate(puzzle, k, n);%0A        for(int i=0; i%3Ccandidate.length(); i++) %7B%0A            if(candidate.charAt(i) == 'Q') %7B%0A                puzzle%5Bk%5D%5Bi%5D = 'Q';%0A                backtrack(puzzle, k+1, n);%0A                puzzle%5Bk%5D%5Bi%5D = '.';%0A            %7D%0A        %7D%0A    %7D%0A    %0A    private String constructCandidate(char%5B%5D%5B%5D puzzle, int k, int n) %7B%0A        String s = %22%22;%0A        for(int i=0; i%3Cn; i++) %7B%0A            if(validPosition(puzzle, k, i, n))  s += %22Q%22;%0A            else s += %22.%22;%0A        %7D%0A        return s;%0A    %7D%0A    %0A    private boolean validPosition(char%5B%5D%5B%5D puzzle, int i, int j, int n) %7B%0A        for(int k=0; k%3Cn; k++) if(k!=i && puzzle%5Bk%5D%5Bj%5D == 'Q') return false;%0A        for(int a=i-1,b=j-1; a%3E=0 && b%3E=0; a--,b--) if(puzzle%5Ba%5D%5Bb%5D == 'Q') return false;%0A        for(int a=i+1,b=j+1; a%3Cn && b%3Cn; a++,b++) if(puzzle%5Ba%5D%5Bb%5D == 'Q') return false;%0A        for(int a=i-1,b=j+1; a%3E=0 && b%3Cn; a--,b++) if(puzzle%5Ba%5D%5Bb%5D == 'Q') return false;%0A        for(int a=i+1,b=j-1; a%3Cn && b%3E=0; a++,b--) if(puzzle%5Ba%5D%5Bb%5D == 'Q') return false;%0A        return true;%0A    %7D%0A    %0A    private boolean is_a_solution(int k, int n) %7B%0A        return k == n;%0A    %7D%0A    %0A    private List%3CString%3E process_solution(char%5B%5D%5B%5D puzzle, int n) %7B%0A        List%3CString%3E list = new ArrayList%3CString%3E();%0A        for(int i=0; i%3Cn; i++) %7B%0A            String s = %22%22;%0A            for(int j=0; j%3Cn; j++) %7B%0A                s+=puzzle%5Bi%5D%5Bj%5D;%0A            %7D%0A            list.add(s);%0A        %7D%0A        return list;%0A    %7D%0A%7D%0A

Create N-Queens_II.java