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的更多相关文章

  1. Java实现 LeetCode 52 N皇后 II

    52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...

  2. [LeetCode] 52. N皇后 II

    题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...

  3. Leetcode之回溯法专题-52. N皇后 II(N-Queens II)

    Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...

  4. leetcode 51. N皇后 及 52.N皇后 II

    51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...

  5. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  6. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  7. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  8. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  9. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

  10. LeetCode - 52. N-Queens II

    52. N-Queens II Problem's Link --------------------------------------------------------------------- ...

随机推荐

  1. mybatis 传入多个参数

    一.单个参数: public List<XXBean> getXXBeanList(@param("id")String id); <select id=&quo ...

  2. SpringBoot------全局异常捕获和自定义异常

    1.添加Maven依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...

  3. 8 -- 深入使用Spring -- 6...3 使用@Transactional

    8.6.3 使用@Transactional Spring还允许将事务配置放在Java类中定义,这需要借助于@Transactional注解,该注解即可用于修饰Spring Bean类,也可用于修饰B ...

  4. 5. RAMN备份与恢复

    一. rman简介 RMAN(Recovery Manager)是一种用于备份(backup).还原(restore)和恢复(recover)数据库的 Oracle 工具.RMAN只能用于ORACLE ...

  5. ORA-01841: (full) year must be between -4713 and +9999,

    OGG报错日志: 2018-09-21 08:52:39 WARNING OGG-01003 Oracle GoldenGate Delivery for Oracle, rep_1b.prm: Re ...

  6. Unity3D 批处理场景的工具

    //场景的批量处理器 public static class OperateScene { public const string SceneDir = "Assets/Scene/&quo ...

  7. flask下载文件中文IE,Edge,Safari文件名乱码

    flask(0.11.2)+python3.6 兼容各个主流浏览器,已经过各种测试(chrome,firefox,safari,IE,Edge) quote是将文件名urlencode化,然后以适应E ...

  8. Qt 4.8.6 PCL 1.8.0 VS 2010 联合编译常见错误

    在Qt和PCL联合编译的过程中,会出现各种各样的错误,解决这些错误的过程真是痛苦万分,所以总结一些常见错误方便自己也方便他人.比如我们要编译PCL1.8.0中的apps中的point_cloud_ed ...

  9. 使用 systemctl 创建 ss 开机

    有自启动脚本.可以设置开机自启. 下载python 安装 ss就不说了.使用 systemctl 创建ss开机自启服务. 创建配置文件 vim /usr/lib/systemd/system/shad ...

  10. matplotlib --> r`$...$`

    文档中介绍的很详细:https://matplotlib.org/tutorials/text/mathtext.html matplotlib  Tutoials --> Text --> ...