https://oj.leetcode.com/problems/n-queens/
http://blog.csdn.net/linhuanmars/article/details/20667175
public class solution { public list<string[]> Solvenqueens (int n) { List<String[]> result = new ArrayList<> (); int[] qcols = new int[n]; solve (N, 0, qcols, result); return result; } private void solve (Int n, // number of queen int row, // current row iteration, [0 -> n-1] int[] qcols, // describing queens positions List< String[]> result) { if (row == n) { // all [0, n-1] has fulfilled, a solution found. result.add (ToBoard (Qcols)) ; return; } // we&nbSp;are on row // try to put q in each columns, and check for ( int i = 0 ; i < n ; i ++) { qcols[row] = i; boolean valid = checkboard (Qcols, row); if (valid) { // enter next iteration solve (n, Row + 1, qCols, result); } } } // This method checks to see if the new Q (ROW) is in line with the existing board // because one row only holds the position of a Q, so the line must conform to // Check column:qcols[i] != qcols[row] // check diagonal abs (I - row) != abs (Qcols[i] - qcols[row]) // check new q (on row) can fit existing board. private boolean Checkboard (Int[] qcols, int row) { for (int i = 0 ; i < row ; i ++) { if (QCOLS[I]&NBSP;==&NBsp;qcols[row]) | | math.abs (i - row) == math.abs (qcols[i] - qcols[row)) return false; } return true; } // Build the board. private String[] toboard (Int[] qcols) { int n = qcols.length; string[] b = new String[n]; for (int i = 0 ; i < n ; i ++) { int qcol = qcols[i]; StringBuilder sb = new StringBuilder (); for (int j = 0 ; j < qcol ; j ++) sb.append ("."); sb.append ("Q"); for (int j = qcol + 1 ; j < n ; j ++) sb.append ("."); b[i] = sb.tostring (); } return b; }}
[Leetcode]51 N-queens