LeetCode_51totalNQueens [N-Queens II]

#pragma warning(disable:4996)

#include <Windows.h>
#include <tchar.h>
#include <cstdio>

/*
	submit time : 1
	request :
		Follow up for N-Queens problem.

		Now, instead outputting board configurations, return the total number of distinct solutions.
*/

void Swap(int* data, int i, int j) {
	int temp = data[i];
	data[i] = data[j];
	data[j] = temp;
}

void totalNQueensRecursively(int& total, int* data, int length, int index) {
	if (index == length - 1) {
		bool bPossible = true;
		for (int i = 0; i < length; ++i) {
			for (int j = i + 1; j < length; ++j) {
				if (i - j == data[i] - data[j] || i - j == data[j] - data[i]) {
					bPossible = false;
					break;
				}
				if (!bPossible)
					break;
			}
		}
		if (bPossible)
			++total;
	}
	else {
		int vernier = index;
		while (vernier < length) {
			Swap(data, vernier, index);
			totalNQueensRecursively(total, data, length, index + 1);
			Swap(data, vernier, index);
			++vernier;
		}
	}
}

int totalNQueens(int n) {
	if (n == 0) return 0;
	if (n == 1) return 1;

	int total = 0;
	int* data = new int[n];
	for (int i = 0; i < n; ++i)
		data[i] = i;

	totalNQueensRecursively(total, data, n, 0);

	return total;
}

//===============Test================
void Test(int n) {
	int total = totalNQueens(n);
	printf("%d\n", total);
}

int _tmain(int argc, _TCHAR* argv[]) {
	Test(5);

	system("pause");
	return 0;
}

LeetCode_51totalNQueens [N-Queens II]

时间: 2024-07-30 05:21:48

LeetCode_51totalNQueens [N-Queens II]的相关文章

lintcode 中等题:N Queens II N皇后问题 II

题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递归的时候才能保存结果,参考程序 java程序: class Solution { /** * Calculate the total number of distinct N-Queen solutions. * @param n: The number of queens. * @return:

58. N-Queens &amp;&amp; N-Queens II

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur

[LeetCode] “全排列”问题系列(一) - 用交换元素法生成全排列及其应用,例题: Permutations I 和 II, N-Queens I 和 II,数独问题

转:http://www.cnblogs.com/felixfang/p/3705754.html 一.开篇 Permutation,排列问题.这篇博文以几道LeetCode的题目和引用剑指offer上的一道例题入手,小谈一下这种类型题目的解法. 二.上手 最典型的permutation题目是这样的: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the foll

LeetCode:N-Queens I II(n皇后问题)

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur

Leetcode | N-Queens I &amp; II

N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board config

N-Queens I&amp;&amp;II

The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of

惊叹计算机运行速度的提升---以n Queens 问题为例

1 介绍 实现了书<Data Structures and Program design in C++>(Robert L. Kruse and Alexander J. Ryba, 2000)中的188页的基于回溯策略的递归算法solve_from,该算法能够计算n Queens问题的解.选择不同的n作为棋盘大小,能够得出不同棋盘大小的Queens问题的解即执行时间. 该书出版时间为2000年,那么使用的计算机大概为1999年左右的.该书给出了执行的结果数据.我在我的电脑上採用相同的代码和算

leetCode 52.N-Queens II (n皇后问题II) 解题思路和方法

N-Queens II Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 思路:解决了上题,这题也就迎刃而解,或者说这题要不上题还要简单一些. 具体代码如下: public class Solution { int count = 0; public int totalNQueens(int n)

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法