package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/n-queens/
 */
public class Problem51 {
    public List<List<String>> solveNQueens(int n) {
        List<List<String>> result = new ArrayList<>();
        // TODO
        return result;
    }

    public static void main(String[] args) {
        Problem51 prob = new Problem51();
        List<List<String>> result = prob.solveNQueens(4);
        for (List<String> row : result) {
            System.out.println(row);
        }
    }
}

@@ -259,279 +259,1534 @@
     
-// TODO%0A        return result;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem51 prob = new Problem51();%0A        List%3CList%3CString%3E%3E result = prob.solveNQueens(4);%0A        for (List%3CString%3E row : result) %7B%0A            System.out.println(row);%0A        %7D
+char%5B%5D%5B%5D board = new char%5Bn%5D%5Bn%5D;%0A        for (int i = 0; i %3C board.length; i++) %7B%0A            for (int j = 0; j %3C board%5Bi%5D.length; j++) %7B%0A                board%5Bi%5D%5Bj%5D = '.';%0A            %7D%0A        %7D%0A        nQueens(board, 0, result);%0A        return result;%0A    %7D%0A%0A    private void nQueens(char%5B%5D%5B%5D board, int row, List%3CList%3CString%3E%3E result) %7B%0A        if (row == board.length) %7B%0A            List%3CString%3E solution = new ArrayList%3C%3E();%0A            for (char%5B%5D chars : board) %7B%0A                solution.add(new String(chars));%0A            %7D%0A            result.add(solution);%0A            return;%0A        %7D%0A        for (int col = 0; col %3C board%5Brow%5D.length; col++) %7B%0A            if (!isPossible(board, row, col)) %7B%0A                continue;%0A            %7D%0A            board%5Brow%5D%5Bcol%5D = 'Q';%0A            nQueens(board, row + 1, result);%0A            // reset everything%0A            board%5Brow%5D%5Bcol%5D = '.';%0A        %7D%0A    %7D%0A%0A    private boolean isPossible(char%5B%5D%5B%5D board, int row, int col) %7B%0A        // up row%0A        for (int i = row; i %3E= 0; i--) %7B%0A            if (board%5Bi%5D%5Bcol%5D == 'Q') %7B%0A                return false;%0A            %7D%0A        %7D%0A        // left diagonal%0A        for (int i = row, j = col; i %3E= 0 && j %3E= 0; i--, j--) %7B%0A            if (board%5Bi%5D%5Bj%5D == 'Q') %7B%0A                return false;%0A            %7D%0A        %7D%0A        // right diagonal%0A        for (int i = row, j = col; i %3E= 0 && j %3C board.length; i--, j++) %7B%0A            if (board%5Bi%5D%5Bj%5D == 'Q') %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;
 %0A   

Solve problem 51