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) {
		int[] x = new int[n];
		queens(x, n, 0);
		return count;
	}

	void queens(int[] x,int n,int row){
		for(int i = 0; i < n; i++){
			if(check(x,n,row,i)){//判断合法
				x[row] = i;//将皇后放在第row行,第i列
				if(row == n-1){//如果是最后一行,则输出结果
					count++;
					x[row] = 0;//回溯,寻找下一个结果
					return;
				}
				queens(x, n, row+1);//寻找下一行
				x[row] = 0;//回溯
			}
		}
	}

	/**
	 * @param x 数组解
	 * @param n 棋盘长宽
	 * @param index 当前放置行
	 * @param i 当前放置列
	 * @return
	 */
	boolean check(int[] x,int n,int row, int col){
		for(int i = 0; i < row; i++){
			if(x[i] == col || x[i] + i == col + row || x[i] - i == col - row)
				return false;
		}
		return true;
	}
}

绝招:这题还有一个非常简单的解法,不看下面代码你能想出来吗?

—————————————————————————————

代码如下:

public class Solution {
	//棋盘长宽在10之内的全部结果数
	int[] x = new int[]{0,1,0,0,2,10,4,40,92,352,724};
	public int totalNQueens(int n) {
		return x[n];
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 20:13:43

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

leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

leetCode 29.Divide Two Integers (两整数相除) 解题思路和方法

Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT. 思路:这个题算法上不是很难,但是通过率相当低,只有15%,果然,自己在写完之后,各种出错,而且错误不是算法上的错误,是各种边界值没有考虑到,很多溢出错误等.下面是具体代码,有详细注释. public class Solution { p

leetCode 32.Longest Valid Parentheses (有效的最大括号) 解题思路和方法

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length

leetCode 71.Simplify Path(化简路径) 解题思路和方法

Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider

leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法

Pow(x, n) Implement pow(x, n). 思路:题目不算难,但是需要考虑的情况比较多. 具体代码如下: public class Solution { public double myPow(double x, int n) { boolean isMin0 = true;//结果负号 if(x > 0 || (n&1) == 0){//x>0或n为偶数 isMin0 = false;//为正 } x = x < 0 ? -x:x;//将x统一设为正值 dou

n皇后2种解题思路与代码-Java与C++实现

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲了n皇后问题的解题思路,并分别用java和c++实现了过程,最后,对于算法改进,使用了位运算. 一.问题抛出与初步解题思路 问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在 8×8 的国际象棋棋盘上放置八个皇后,使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上. 转化规则:其实八皇后问题可以推广为更一般的n皇后

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:

leetcode#52 N queensⅡ

n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: 输入: 4 输出: 2 解释: 4 皇后问题存在如下两个不同的解法. [  [".Q..",  // 解法 1   "...Q",   "Q...",   "..Q."],  ["..Q.",  // 解法 2  

leetCode 51.N-Queens (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