[LeetCode]51. N-Queens&52. N-Queens II

51 . N-Queens
Hard

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens’ placement, where ‘Q’ and ‘.’ both indicate a queen and an empty space respectively.

For example,
There exist two distinct solutions to the 4-queens puzzle:

[
[“.Q..”, // Solution 1
“…Q”,
“Q…”,
“..Q.”],

[“..Q.”, // Solution 2
“Q…”,
“…Q”,
“.Q..”]
]

9ms:

 public List<List<String>> solveNQueens(int n) {  
            List<List<String>> res = new ArrayList<List<String>>();  
            int[] queenList = new int[n]; //第i个位置存放的数表示row行时,Q的列  
            placeQueen(queenList, 0, n, res);//在第0行放Q  
            return res;  
        }  
        private void placeQueen(int[] queenList, int row, int n, List<List<String>> res) {  
            //如果已经填满,就生成结果  
            if (row == n) {  
                ArrayList<String> list = new ArrayList<String>();  
                for (int i = 0; i < n; i++) {  
                    String str = "";  
                    for (int col = 0; col < n; col++){  
                        if(queenList[i] == col) {  
                            str += "Q";  
                        } else {  
                            str += ".";  
                        }  
                    }  
                    list.add(str);  
                }  
                res.add(list);  
            }  
            for (int col = 0; col < n; col++) {//循环每一列  
                if (isValid(queenList, row, col)) { //如果在该列放入Q不冲突的话  
                    queenList[row] = col;  
                    placeQueen(queenList, row + 1, n, res);  
                }  
            }  
        }  

        private boolean isValid(int[] queenList, int row, int col) {  
            for (int i = 0; i < row; i++) {  
                int pos = queenList[i];  
                if (pos == col) { //和新加入的Q处于同一列  
                    return false;  
                }  
                if (pos + row - i == col) { //在新加入的Q的右对角线上  
                    return false;  
                }  
                if (pos - row + i == col) { //在新加入的Q的左对角线上  
                    return false;  
                }  
            }  
            return true;  
        } 

52 . N-Queens II
Hard

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

public class Solution {
    int count = 0;
    public int totalNQueens(int n) {
        boolean[] cols = new boolean[n];     // columns   |
        boolean[] d1 = new boolean[2 * n];   // diagonals \
        boolean[] d2 = new boolean[2 * n];   // diagonals /
        backtracking(0, cols, d1, d2, n);
        return count;
    }

    public void backtracking(int row, boolean[] cols, boolean[] d1, boolean []d2, int n) {
        if(row == n) count++;

        for(int col = 0; col < n; col++) {
            int id1 = col - row + n;
            int id2 = col + row;
            if(cols[col] || d1[id1] || d2[id2]) continue;

            cols[col] = true; d1[id1] = true; d2[id2] = true;
            backtracking(row + 1, cols, d1, d2, n);
            cols[col] = false; d1[id1] = false; d2[id2] = false;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值