LeetCode52:N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
和N-Queens 同樣的解法。<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxwcmUgY2xhc3M9"brush:java;">
class Solution {
public:
int totalNQueens(int n) {
vector > result;
vector nums;
for(int i=0;i > base=permutation(nums);
return base.size();
}
vector > permutation(vector &nums)
{
vector >result;
permutationChild(nums,0,result);
return result;
}
void permutationChild(vector &nums,int offset,vector >&result)
{
if(offset==nums.size()-1)
{
if(judge(nums))
result.push_back(nums);
return;
}
auto base=nums.begin()+offset;
auto iter=base;
for(;iter!=nums.end();iter++)
{
swap(*base,*iter);
permutationChild(nums,offset+1,result);
swap(*base,*iter);
}
}
bool judge(vector & nums)
{
int length=nums.size();
for(int i=0;i