题目链接: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. git开发实战:认识git

    git简介: git是分布式版本控制系统,相比较svn相比,git会在本地保存完整的提交记录,即使远程服务器宕机数据消失,可以将本地分支提交到远程服务器,本地分支会保存完整的记录.只要文件提交到git ...

  2. Reciteing(first)

      it is sybmbolically portrayed in this cartoon,when a teacher assigns her student to read a literat ...

  3. Python中单下划线和双下划线

    1.双下划线开头和结尾 Python中存在一些特殊的方法,有些方法以双下划线 “__” 开头和结尾,它们是Python的魔法函数,比如__init__()和__str__等等.不用要这种方式命名自己的 ...

  4. Ubuntu18.10下出现Could not get lock /var/lib/dpkg/lock的错误

    最近在Windows10系统下使用Oracle VM VirtualBox6.0.2下安装的Ubuntu18.10时,运用sudo  apt-get install 安装pkg-config工具,在终 ...

  5. if语句基本结构以及基础案例演示

    1.结构 if(比较表达式1) { 语句体1; }else if(比较表达式2) { 语句体2; }else if(比较表达式3) { 语句体3; } ... else { 语句体n+1; } 2.执 ...

  6. luoguP2680 运输计划 题解(二分答案+树上差分)

    P2680 运输计划  题目 这道题如果是看的我的树上差分来的,那么肯定一看题目就可以想到树上差分. 至于这是怎么想到的,一步一步来: 1.n有300000,不可能暴力枚举每一条边 2.因为我们要使运 ...

  7. static的变量是放在哪里

    static的变量都放在数据段,但是初始值若为0则放在BSS节中.而初始值非零则放在数据节中. 数据节和BSS节都属于数据段.   顺便说说对象的存储,可分为三类:静态存储(static storag ...

  8. OAuth授权登录

    一.写在前面 日常生活中,我们经常看到到一个网站时,需要登录的时候,都提供了第三方的登录,也就是说你可以使用你的微信,QQ,微博等账号进行授权登录.那么这个认证登录的东西到底是什么呢? 微信授权登录页 ...

  9. 从Flask-Script迁移到Flask-Cli

    Abstrct flask从0.11版本开始引入了click提供命令行支持,在此之前我们通常会引入Flask-Script来提供. 在<Flask web开发>这本书编写时flask0.1 ...

  10. [HTML知识体系]语义化标签概论

    1.什么是语义化 语义化(Semantic)在HTML5中被大量提起,就是语义化标签向浏览器和开发者展示了它所包裹内容的类型与意思,可是至今我看了好多代码,HTML5新增的语义化标签的使用率还是挺低的 ...