<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>
Given an integer
Each solution contains a distinct board configuration ofthe 'Q'
'.'
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 {
}