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 --------------------------------------------------------------------- ...
随机推荐
- 驱动文件中只有cat/inf/dll文件,怎么安装
网上下载了一个驱动,里面包含文件只有cat/inf/dll文件,怎么安装? 1.计算机-右键-管理-设备管理器,找到要装驱动的设备上 2.右键-更新驱动程序-浏览到本地的这个驱动文件夹 3.开始安装
- How to get all Errors from ASP.Net MVC modelState?
foreach (ModelState modelState in ViewData.ModelState.Values) { foreach (ModelError error in modelSt ...
- 多线程开发之三 GCD
NSThread.NSOperation.GCD 总结: 无论使用哪种方法进行多线程开发,每个线程启动后并不一定立即执行相应的操作,具体什么时候由系统调度(CPU 空闲时就会执行) 更新 UI 应该在 ...
- docker_File 执行报错总结
编写dockerfile [root@linux-node1 ~/dk]# cat Dockerfile # this is a docker File FROM centos MAINTAINER ...
- MyBatis Generator使用com.mysql.cj.jdbc.Driver遇到的问题
MyBatis Generator使用com.mysql.cj.jdbc.Driver Mybatis Generator 1.3.5 新建了一个decision库,并创建了一张user表 impor ...
- 解决java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone
使用spring boot整合MySQL时一直报 java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecog ...
- Tomcat -- 启动错误 -- 解决锦集
java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter :在Tomacat7的context.xml文 ...
- ctrl c 中文字符到 vnc 里,中文字符已经被转码
为了测试程序对多语言字符的支持情况,我找来一段中文和北欧的文字,希望把这些文字上传到elasticsearch,并能正确显示. 首先测试了北欧文字,一切OK. 但是中文复制到 VNC 客户端(Linu ...
- IntelliJ IDEA 激活 及 License Server 安装使用 Window篇
IDEA版本: IntelliJ IDEA 2017.2Build #IU-172.3317.76, built on July 15, 2017Licensed to Administrator J ...
- Mac OSX安装启动 zookeeper
安装 zookeeper支持brew安装 ➜ ~ brew info zookeeper zookeeper: stable (bottled), HEAD Centralized server fo ...