标签:
题目来源:
https://leetcode.com/problems/n-queens-ii/
题意分析:
这题和上一题是一样的。上一题是输出所有满足N-后的结果,这题是输出所有结果的个数。
题目思路:
和上题一样,只需要将答案改为个数。
代码(python):
class Solution(object): def totalNQueens(self, n): """ :type n: int :rtype: int """ board = [-1 for i in range(n)] ans = [] def isqueens(depth,j): for i in range(depth): if board[i] == j or abs(depth - i) == abs(board[i] - j): return False return True def dfs(depth): if depth == n: ans.append(0);return for i in range(n): if isqueens(depth,i): board[depth]= i dfs(depth + 1) dfs(0) return len(ans)
转载请注明出处:http://www.cnblogs.com/chruny/p/4968673.html
[LeetCode]题解(python):052-N-Queens II
标签:
原文地址:http://www.cnblogs.com/chruny/p/4968673.html