Queens

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> </span><wbr style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">The</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: rgb(255, 255, 255);">n</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">-queenspuzzle is the problem of placing</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: rgb(255, 255, 255);">n</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">queenson an</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: rgb(255, 255, 255);">n</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">�</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; line-height: 1.5; margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: baseline; background-color: rgb(255, 255, 255);">n</span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);"> <wbr></wbr></span><span style="color: rgb(34, 34, 34); font-family: 'Helvetica neue', arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255);">chessboardsuch that no two queens attack each other.</span></wbr>

N <wbr>Queens

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

Each solution contains a distinct board configuration ofthe n-queens'placement, where 'Q' and '.' bothindicate 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.."]
]

2014.8.11 update:

    public List<String[]> solveNQueens(int n) {
        List<Integer> row = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            row.add(i);
        }
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        helper(result, row, new ArrayList<Integer>(), n);
        
        List<String[]> res = new ArrayList<String[]>();
        int rowNum;
        for (List<Integer> col : result) {
            rowNum = 0; // 循环完了要重新初始!
            String[] rowStr = new String[n];
            for (Integer i : col) { // process one possibility
                String tmp = "";
                for (int j = 0; j < n; j++) { // construct one row string "..Q."
                    if (i.intValue() == j) {
                        tmp = tmp + "Q";
                    } else {
                        tmp = tmp + ".";
                    }
                }
                rowStr[rowNum++] = tmp;
            }
            res.add(rowStr);
        }
        return res;
    }


    void helper(List<List<Integer>> result, List<Integer> row, List<Integer> col, int n) {
        if (col.size() == n) {
            result.add(col);
        }
        
        for (int i = 0; i < n; i++) {
            List<Integer> newCol = new ArrayList<Integer>(col);
            newCol.add(i);
            if (isValid(row, newCol)) {
                helper(result, row, newCol, n);
            }
        }
    }
    


    boolean isValid(List<Integer> row, List<Integer> col) {
        // process col
        Integer lastElement = col.get(col.size()-1);
        for (int i = 0; i < col.size() - 1; i++) {
            if (lastElement.equals(col.get(i))) {
                return false;
            }
            if (Math.abs(i - (col.size()-1)) == Math.abs(col.get(i) - lastElement)) {  // 判断对角线时要row[i] - row[last], col[i] - col[last] 低级错误是row-col
                return false;
            }
        }
        return true;
    }
}



2013:
public class Solution {
    public ArrayListsolveNQueens(int n) {
        // Start typing your Java solution below
        // DO NOT write main() function
        ArrayList result = new ArrayList();

        ArrayList validRows = new ArrayList();
        Integer[] col = new Integer[n];
        for (int i = 0; i < n; i++){
            col[i] =-1;
        }
        helper(n, 0, col, validRows);

        for (int i = 0; i <validRows.size(); i++) {
            Integer[]cur = validRows.get(i);
            String[]oneResult = new String[n];

            //construct each matrix
            // j: rowk: col
            for (int j= 0; j < n; j++) {
                // construct row
                String row = "";
                for (int k = 0; k< n; k++) {
                    if (k == cur[j]) {
                        row = row+ "Q";
                    } else {
                        row = row+ ".";
                    }
                }
                oneResult[j] = row;
            }
            result.add(oneResult);
        }
        return result;
    }

    void helper(int n, introw, Integer[] col, ArrayList validRows) {
        if (row == n) {
            validRows.add(col.clone());
            return;
        }
        for (int targetCol = 0; targetCol< n; targetCol++) {
            if(isValid(col, row, targetCol)) {
                col[row] = targetCol;
                helper(n, row+1, col,validRows);
            }
        }

    }
     
    booleanisValid(Integer[] col, int row, int targetCol) {
        // not in the same col
        for (int row1 = 0; row1 < row;row1++) {
            int col1 =col[row1];
            if(targetCol == col1) {
                return false;
            }
         
        // not in the same diagnal
            if(Math.abs(row - row1) == Math.abs(targetCol - col1)) {
                return false;
            }
        }
         
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值