题目链接:https://leetcode-cn.com/problems/n-queens/

题目链接:https://leetcode-cn.com/problems/n-queens-ii/

题目大意:

  略。

分析:

代码如下:

 class Solution {
public:
int allOne;
vector<vector<string>> ans;
vector<string> ret;
string tmp;
int N; vector<vector<string>> solveNQueens(int n) {
allOne = ( << n) - ;
ans.clear();
tmp.assign(n, '.');
ret.assign(n, tmp);
N = n; solve(, , , ); return ans;
} // vd : 竖直方向
// ld : 左斜线方向
// rd : 右斜线方向
void solve(int level, int vd, int ld, int rd) {
if(level == N) {
ans.push_back(ret);
return;
} int limit = (vd | ld | rd) ^ allOne;
int pos; while(limit) {
pos = lowbit(limit);
limit = cutLowbit(limit); ret[level][N - __builtin_ffs(pos)] = 'Q';
solve(level + , vd | pos, ((ld | pos) >> ) & allOne, ((rd | pos) << ) & allOne);
ret[level][N - __builtin_ffs(pos)] = '.';
}
} int lowbit(int x) {
return x & (-x);
} int cutLowbit(int x) {
return x & (x - );
}
};
 class Solution {
public:
int allOne;
int ans;
int N; int totalNQueens(int n) {
allOne = ( << n) - ;
ans = ;
N = n; solve(, , , ); return ans;
} // vd : 竖直方向
// ld : 左斜线方向
// rd : 右斜线方向
void solve(int level, int vd, int ld, int rd) {
if(level == N) {
++ans;
return;
} int limit = (vd | ld | rd) ^ allOne;
int pos; while(limit) {
pos = lowbit(limit);
limit = cutLowbit(limit); solve(level + , vd | pos, ((ld | pos) >> ) & allOne, ((rd | pos) << ) & allOne);
}
} int lowbit(int x) {
return x & (-x);
} int cutLowbit(int x) {
return x & (x - );
}
};

LeetCode N皇后 & N皇后 II的更多相关文章

  1. 乘风破浪:LeetCode真题_040_Combination Sum II

    乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...

  2. [LeetCode] 445. Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  3. Leetcode:面试题68 - II. 二叉树的最近公共祖先

    Leetcode:面试题68 - II. 二叉树的最近公共祖先 Leetcode:面试题68 - II. 二叉树的最近公共祖先 Talk is cheap . Show me the code . / ...

  4. Leetcode:面试题55 - II. 平衡二叉树

    Leetcode:面试题55 - II. 平衡二叉树 Leetcode:面试题55 - II. 平衡二叉树 Talk is cheap . Show me the code . /** * Defin ...

  5. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  6. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  7. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  8. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  9. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

随机推荐

  1. cita 源码研究

    适用环境 vim + YouCompleteMe 使用 github 源,不能使用 ustc 源 git clone --depth 1 --recusive https://github.com/k ...

  2. maven(二),Linux安装maven3.5.3及配置

    Linux系统,ubuntu-16.04.4,安装maven3.5.3 一.创建文件夹 注意Linux用户,这个如果不是root用户,命令前面需要加:sudo //创建一个目录 mkdir /usr/ ...

  3. 【Python—windows 下 virtualEnv 使用】

    用pip安装virtualenv pip3 install virtualenv 在相应的文件夹中创建一个独立的Python运行环境,命名为env. 之后会自动创建一个 env 文件夹,有: Incl ...

  4. DNS 放大

    DNS放大攻击是伪造一个DNS查询的报文,源地址改成想要攻击的IP.单个查询的包64字节,如果是ANY类型查询(或者DNSSEC记录),那么回复报文一般会大几十倍.当然,如果攻击者自己制造一个很大的T ...

  5. this.$router.push相关的vue-router的导航方法

    this.$router.push相关的vue-router的导航方法:https://blog.csdn.net/zeroyulong/article/details/80312750

  6. C#隐式类型和显示类型

    一,在程序中我们经常会遇到:无法将类型“XXX”隐式装换为“XXX”,如下例子: static void Main(string[] args) { int i; i = "Hello Wo ...

  7. VUE $SET源码

  8. 【记录】解决uni-app 用nginx反向代理出现Invalid Host header问题

    之前解决过一次,后来给忘记了,今天又遇到这个问题,现记录一下 修改uni-app的manifest.json文件  - >源码视图 添加以下代码: "disableHostCheck& ...

  9. 检测ip是否通过

    #!/bin/bashnetstat -an |grep "ESTABLISHED" |awk '{print $4}' |awk -F ':' '{print $1}' |sor ...

  10. 【多线程】LinkedTransferQueue

    LinkedTransferQueue是JDK1.7才添加的阻塞队列,基于链表实现的FIFO无界阻塞队列,是ConcurrentLinkedQueue(循环CAS+volatile 实现的wait-f ...