https://oj.leetcode.com/problems/n-queens-ii/
http://blog.csdn.net/linhuanmars/article/details/20668017
Same as N-Queens.
public class Solution { public int totalNQueens(int n) { Result result = new Result(); int[] qcols = new int[n]; resolve(n, 0, qcols, result); return result.value; } private void resolve(int n, int row, int[] qcols, Result result) { if (row >= n) { result.value ++; return; } for (int i = 0 ; i < n ; i ++) { qcols[row] = i; if (check(qcols, row)) { resolve(n, row + 1, qcols, result); } } } private static class Result { int value; } private boolean check(int[] qcols, int row) { for (int i = 0 ; i < row ; i ++) { if ((qcols[i] == qcols[row]) || (Math.abs(i - row) == Math.abs(qcols[i] - qcols[row]))) return false; } return true; } }
原文地址:http://7371901.blog.51cto.com/7361901/1598587