Queens

来源:互联网 发布:熹妃传刷元宝软件 编辑:程序博客网 时间:2024/05/29 21:33
<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.."]]
位运算神解:http://www.matrix67.com/blog/archives/266

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
原创粉丝点击