首页 > 代码库 > [LeetCode]52 N-Queens II
[LeetCode]52 N-Queens II
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; } }
[LeetCode]52 N-Queens II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。