首页 > 其他分享 >【刷题笔记】52. N-Queens II

【刷题笔记】52. N-Queens II

时间:2023-09-17 21:35:58浏览次数:41  
标签:index int res 52 II Queens dia2 col row

题目

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 the number of distinct solutions to the n-queens puzzle.

Example:

Input: 4
Output: 2
Explanation: There are two distinct solutions to the 4-queens puzzle as shown below.
[
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]

题目大意

给定一个整数 n,返回 n 皇后不同的解决方案的数量。

解题思路

  • 这一题是第 51 题的加强版,在第 51 题的基础上累加记录解的个数即可。
  • 这一题也可以暴力打表法,时间复杂度为 O(1)。

参考代码

package leetcode

// 解法一,暴力打表法
func totalNQueens(n int) int {
	res := []int{0, 1, 0, 0, 2, 10, 4, 40, 92, 352, 724}
	return res[n]
}

// 解法二,DFS 回溯法
func totalNQueens1(n int) int {
	col, dia1, dia2, row, res := make([]bool, n), make([]bool, 2*n-1), make([]bool, 2*n-1), []int{}, 0
	putQueen52(n, 0, &col, &dia1, &dia2, &row, &res)
	return res
}

// 尝试在一个n皇后问题中, 摆放第index行的皇后位置
func putQueen52(n, index int, col, dia1, dia2 *[]bool, row *[]int, res *int) {
	if index == n {
		*res++
		return
	}
	for i := 0; i < n; i++ {
		// 尝试将第index行的皇后摆放在第i列
		if !(*col)[i] && !(*dia1)[index+i] && !(*dia2)[index-i+n-1] {
			*row = append(*row, i)
			(*col)[i] = true
			(*dia1)[index+i] = true
			(*dia2)[index-i+n-1] = true
			putQueen52(n, index+1, col, dia1, dia2, row, res)
			(*col)[i] = false
			(*dia1)[index+i] = false
			(*dia2)[index-i+n-1] = false
			*row = (*row)[:len(*row)-1]
		}
	}
	return
}

// 解法三 二进制位操作法
// class Solution {
// 	public:
// 		int totalNQueens(int n) {
// 			int ans=0;
// 			int row=0,leftDiagonal=0,rightDiagonal=0;
// 			int bit=(1<<n)-1;//to clear high bits of the 32-bit int
// 			Queens(bit,row,leftDiagonal,rightDiagonal,ans);
// 			return ans;
// 		}
// 		void Queens(int bit,int row,int leftDiagonal,int rightDiagonal,int &ans){
// 			int cur=(~(row|leftDiagonal|rightDiagonal))&bit;//possible place for this queen
// 			if (!cur) return;//no pos for this queen
// 			while(cur){
// 				int curPos=(cur&(~cur + 1))&bit;//choose possible place in the right
// 				//update row,ld and rd
// 				row+=curPos;
// 				if (row==bit) ans++;//last row
// 				else Queens(bit,row,((leftDiagonal|curPos)<<1)&bit,((rightDiagonal|curPos)>>1)&bit,ans);
// 				cur-=curPos;//for next possible place
// 				row-=curPos;//reset row
// 			}
// 		}
// };

标签:index,int,res,52,II,Queens,dia2,col,row
From: https://blog.51cto.com/u_16110811/7504021

相关文章

  • 每天一个linux命令(52):ifconfig命令
    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改。Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config)。通常需要以root身份登录或使用sudo以便在Linux机器上使用ifconfig工具。依赖于ifconfig命令中使用一些选项属性,ifconfig工......
  • Modbus协议详解3:数据帧格式 - RTU帧 & ASCII帧的区别
    Modbus既然是一种通信协议,那它就应该有规定的通信格式用于在设备之间的指令接收与识别。本文就着重讲讲Modbus协议的RTU帧和ASCII帧。Modbus帧在串行链路上的格式如下:在上图的格式中:1)地址域:指代的是子节点地址。合法的子节点地址为 0 – 247。 每个子设备被赋予 1 – 247 ......
  • LearnIing by doing
    Java规划图 第一个月 学习java基础,掌握一些基本的计算机知识,学习编程基础,面向对象,常用类,集合,异常,Io,多线程,网络编程。学习数据库,以及基本的Html和Css第二个月 有了第一个月的基本前端知识,和Java知识后,就可以开始学习JavaWeb了,继续往深入的学习前端知识,学习Oracle以及JDBC......
  • 代码随想录算法训练营-回溯算法-2|55. 跳跃游戏、45. 跳跃游戏 II、1005. K 次取反后
    55. 跳跃游戏1.跳跃的覆盖范围。这个问题就转化为跳跃覆盖范围究竟可不可以覆盖到终点!2. 贪心算法局部最优解:每次取最大跳跃步数(取最大覆盖范围),整体最优解:最后得到整体最大覆盖范围,看是否能到终点。时间复杂度:O(n)空间复杂度:O(1)1classSolution:2defca......
  • CF1852B Imbalanced Arrays 题解
    CF1852BImbalancedArrays题解Links洛谷CodeforcesDescription对于一个给定的长度为\(n\)的数组\(A\),定义一个长度为\(n\)的数组\(B\)是不平衡的当且仅当以下全部条件满足:\(-n\leqB_{i}\leqn\)且\(B_{i}\ne0\)。即每个数在\([-n,n]\)内且不为\(0\)。......
  • [LeetCode] 1222. Queens That Can Attack the King
    Ona 0-indexed 8x8 chessboard,therecanbemultipleblackqueensadonewhiteking.Youaregivena2Dintegerarray queens where queens[i]=[xQueeni,yQueeni] representsthepositionofthe ith blackqueenonthechessboard.Youarealsogivena......
  • leet code 删除有序数组中的重复项 I II
    26.删除有序数组中的重复项80.删除有序数组中的重复项II总结反思这两个题目,虽然难度程度一个是简单,一个是中等,都不是特别难。但是都没有解决。因为这两道题目都是运用双指针解决的,证明自己对双指针的掌握程度还不是很熟练。反思:为什么没有解出来?又或者,经过一段时间之后是否能够......
  • 【代码随想录算法训练营第二天】977.有序数组的平方、209.长度最小的子数组 、59.螺旋
    Day2-数组2023.9.15Leetcode977有序数组的平方给你一个按非递减顺序排序的整数数组nums,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。初解我还是不能想到暴力解法之外的,对某个问题的最优复杂度也没有概念。就算提示我是用指针,我也想不到思路。现在我知......
  • 删除有序数组中的重复项 II
    题目删除有序数组中的重复项II给你一个有序数组nums,请你原地删除重复出现的元素,使得出现次数超过两次的元素只出现两次,返回删除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(1)额外空间的条件下完成。说明:为什么返回数值是整数,但输......
  • bindizip批量压缩解压(xjl456852原创)
    linux使用bindizip需要在wine下使用.linux批量解压缩脚本(xjl456852原创):脚本名: unpack.sh,可以将脚本放入到/usr/bin/bash下进行使用更方便没有加入-y参数,所以解压完成后不会自动关闭窗口,需要自己手动关闭窗口.因为需要检测是否有错误.如果不想检测是否有错误,可以使用下面......