【leetcode】N-queens II

问题:

返回N皇后问题解的个数。

分析:

详见 N-queens

实现:

 bool nextPermutation(vector<int> &num)
{
	int i = num.size() - 1;
	while (i >= 1)
	{
		if(num[i] > num[i - 1])
		{
			--i;
			int ii = num.size() - 1;
			while (ii > i && num[ii] <= num[i]) --ii;
			if(ii > i)
			{
				swap(num[i], num[ii]);
				reverse(num.begin() + i + 1, num.end());
				return true;
			}
		}
		else
			--i;
	}
    return false;
}

bool check(vector<int> &grids)
{
	int len = grids.size();
	for (int i = 0; i < len; ++i)
		for (int j = i + 1; j < len; ++j)
		{
			if(abs(i - j) == abs(grids[i] - grids[j]))
				return false;
		}
	return true;
}
int totalNQueens(int n) {
	if(n < 1)
		return 0;
	if(n == 1)
		return 1;
	int total = 0;
	//init the girds.
	vector<int> grids(n,0);
	for (int i = 0; i < n; ++i)
	{
		grids[i] = i;
	}

	while (nextPermutation(grids))
	{
		if(check(grids))
			total++;
	}
	return total;
}

【leetcode】N-queens II,布布扣,bubuko.com

时间: 2024-10-10 21:15:31

【leetcode】N-queens II的相关文章

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【Leetcode】Unique Paths II

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

【LeetCode】Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

【LeetCode】Course Schedule II 解题报告

[题目] There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses an

【LeetCode】Jump Game II 解题报告

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of

【LeetCode】Unique Paths II 解题报告

[题目] Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the

【LeetCode】Palindrome Partitioning II 解题报告

[题目] Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. For example, given s = "aab", Return 1 since the palindrome partitioning ["aa&