@@ -0,0 +1,1826 @@
+package problems;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Apublic class NQueens %7B%0A    public List%3CString%5B%5D%3E solveNQueens(int n) %7B%0A        List%3CString%5B%5D%3E out = new ArrayList%3CString%5B%5D%3E();%0A        if (n %3C= 0)%0A            return out;%0A%0A        int%5B%5D place = new int%5Bn%5D; // record the queen location at each row.%0A        placeQueens(place, 0, out);%0A        return out;%0A    %7D%0A    %0A    void placeQueens(int%5B%5D place, int row, List%3CString%5B%5D%3E out) %7B%0A        for (int i = 0; i %3C place.length; i++) %7B%0A            place%5Brow%5D = i;%0A            if (!check(place, row))%0A                continue;%0A            %0A            if (row == place.length - 1)%0A                printBoard(place, out);%0A            else%0A                placeQueens(place, row + 1, out);%0A        %7D %0A    %7D%0A    %0A    boolean check(int%5B%5D place, int row) %7B%0A        for (int i = 0; i %3C row; i++) %7B%0A            if (place%5Bi%5D == place%5Brow%5D)%0A                return false;%0A            if (Math.abs(place%5Bi%5D - place%5Brow%5D) == row - i)%0A                return false;%0A        %7D%0A        return true;%0A    %7D%0A    %0A    void printBoard(int%5B%5D place, List%3CString%5B%5D%3E out) %7B%0A        String%5B%5D board = new String%5Bplace.length%5D;%0A        for (int i = 0; i %3C place.length; i++) %7B%0A            board%5Bi%5D = %22%22;%0A            for (int j = 0; j %3C place.length; j++) %7B%0A                if (j != place%5Bi%5D)%0A                    board%5Bi%5D += '.';%0A                else%0A                    board%5Bi%5D += 'Q';%0A            %7D%0A        %7D%0A        out.add(board);%0A    %7D%0A    %0A    public static void main(String%5B%5D args) %7B%0A        NQueens nq = new NQueens();%0A        %0A        List%3CString%5B%5D%3E out = nq.solveNQueens(8);%0A        for (int i = 0; i %3C out.size(); i++) %7B%0A            for (int j = 0; j %3C out.get(i).length; j++)%0A                System.out.println(out.get(i)%5Bj%5D);%0A            System.out.println();%0A        %7D%0A    %7D%0A%7D%0A

Add N Queens