LeetCode-52.N-Queen 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 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.."]
]
使用深度优先遍历,并剪枝
注意斜线上的规律,左斜线上的点 横纵坐标和相同,右斜线上的点 横纵坐标差相同
class Solution {
int total = 0;
public int totalNQueens(int n) { dfs(n,0,new ArrayList<Integer>(),new ArrayList<Integer>(),new ArrayList<Integer>());
return total;
}
private void dfs( int n, int level, List<Integer> cols, List<Integer> sum, List<Integer> dif) {
if (level == n) {
total++;
return;
}
for (int i = 0; i < n; i++) {
if (cols.contains(i) || sum.contains(i + level) || dif.contains(i - level))
continue;
cols.add(i);
sum.add(i + level);
dif.add(i - level);
dfs(n, level + 1, cols, sum, dif);
cols.remove(cols.size() - 1);
sum.remove(sum.size() - 1);
dif.remove(dif.size() - 1);
}
}
}
使用位运算(最优解)
class Solution {//DFS 位运算 mytip
public int totalNQueens(int n) {
int total=0;
return dfs(total,n,0,0,0,0);
//return total;
}
private int dfs(int total, int n, int level, int cols, int pie, int na) {
if (level == n) {
total++;
return total;
}
int bits = (~(cols|pie|na))&((1<<n)-1);//得到可能放的空位
while(0!=bits){//遍历可能放的空位
int cur = bits&(-bits);//得到最后一个1
total= dfs(total,n,level+1,cols|cur,(pie|cur)<<1,(na|cur)>>1);
bits= bits&(bits-1);//清楚最后一个1
}
return total;
}
}
相关题
n皇后 LeetCode51 https://www.cnblogs.com/zhacai/p/10621300.html
LeetCode-52.N-Queen II的更多相关文章
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- [LeetCode] 52. N皇后 II
题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...
- Leetcode之回溯法专题-52. N皇后 II(N-Queens II)
Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...
- leetcode 51. N皇后 及 52.N皇后 II
51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
- LeetCode - 52. N-Queens II
52. N-Queens II Problem's Link --------------------------------------------------------------------- ...
随机推荐
- CentOS 7 设置静态IP
cd /etc/sysconfig/network-scripts/ sudo vi ifcfg-eno16777736 BOOTPROTO=static #dhcp改为static(修改) IPAD ...
- nginx 配置信息
主配置文件: cat /etc/nginx/nginx.conf# For more information on configuration, see:# * Official English Do ...
- [IR] Open Source Search Engines
From:http://blog.csdn.net/xum2008/article/details/8740063 本文档是对现有的开源的搜索引擎的一个简单介绍 1. Lucene Lucene ...
- fs项目---->async/await的学习(一)
2018-07-11号,我来到了fs项目组担任后端开发的角色.这是我来thoughtworks以来首个的正式项目,不管是在技术还是在敏捷的实践中都是受益匪浅.来感受tw特殊的文化的同时,我希望自己能够 ...
- 通过JVM 参数 实现spring 应用的二进制代码与配置分离。
原创文章,转载请注明出处 分离的好处就不说了.说下分离的思路.通过JVM 参数-D 添加 config.path 的property 到系统中.系统通过System.getProperty(confi ...
- android.DataBindingUtil
import android.databinding.DataBindingUtil import android.os.Bundle import android.support.v7.app.Ap ...
- include_once与require_once的区别
①作用及用法 可以减少代码的重复 include(_once)("文件的路径")与require(_once)("文件的路径") ②理解 说白了,就是用包含进 ...
- python数据类型之字典(二)
字典的基本操作 键值查找: >>> aInfo = {'Wangdachui':3000,'Niuyun':2000,'Linling':4500,'Tianqi':8000} &g ...
- Mapper 赋值对应实体属性
public static class MapperExtensions { public static TResult MapTo<TResult>(this object self, ...
- d9
# 整体进度# python基础 ——38天 2个月# 数据库 —— 存储数据和信息用的,本质上和文件没有区别 1-2周 # —— 增删改查更方便了# 前端 —— 2周# 框架 —— django 2 ...