经典的N皇后问题,

这里学到了一个非常牛的新方法(http://www.matrix67.com/blog/archives/266),用位运算来求解N皇后问题;

思路其实也很容易懂,一点都不复杂,

同样是遍历每一行的每一列,只不过所有冲突的位置都用bit位置记录下来了,

首先考虑在第k行的第j列放一个皇后,那么第k + 1行的第j - 1列和 j +1列都会与该皇后冲突,

用位运算的左右移位就能表示所有列的有冲突的位置,那么在选择第k + 1行的放置位置的时候就只能选择

无冲突的位置。

 class Solution {
public:
int totalNQueens(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int res = ;
getNQueens(res, n, , , );
return res;
}
void getNQueens(int &res, int n, int row, int ld, int rd) {
if (row == ( << n) - ) {
++res;
return;
}
int pos = ~(row | ld | rd);
for (int i = ; i < n; ++i) {
int p = << i;
if (pos & p) {
getNQueens(res, n, row | p, (ld | p) << , (rd | p) >> );
}
}
}
};

LeetCode-NQueensII的更多相关文章

  1. leetcode N-QueensII

    题目和上一题一样,就是要求输出有多少种结果.最直接的就是,只要在上一题的代码return ans.size();就可以了.果然也是AC了. 然后我翻看了几种别人写的,暂时还没有找到复杂度可以比上一题降 ...

  2. Leetcode:52. N-QueensII

    Description Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...

  3. leetcode 98:n-queens-ii

    题目描述 继续思考"n-queens"问题 这次我们不是输出皇后的排列情况,而是输出n皇后问题一共有多少种解法 Follow up for N-Queens problem. No ...

  4. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  5. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  6. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  7. [LeetCode]题解(python):052-N-Queens II

    题目来源 https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead outputt ...

  8. <转>LeetCode 题目总结/分类

    原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...

  9. [Leetcode][Python]52: N-Queens II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...

  10. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

随机推荐

  1. JDBC浅析

    今天简单的说一下jdbc,本来这玩意儿也很简单. 大家只需要记住其中的几个重要的类就行了,它们都在sql包里.今天主要是拿mysql来连接.先看一下主要的几个类吧. 1.Conenction 2.St ...

  2. Eclipse中快速 打出 main方法的签名

    有时,我们创建一个空白类,需要打出main方法 public static void main(String [] args){ } 在Eclipse先敲main字符,然后按住ALT+/,再按回车即可 ...

  3. thinkphp的where方法的使用

    1.Thinkphp中where()条件的使用 总是有人觉得,thinkphp的where()就是写我要进行增加.查询.修改.删除数据的条件,很简单的,其实我想告诉你,where()是写条件语句的,但 ...

  4. 【计算几何】【凸包】【极角排序】【二分】Gym - 101128J - Saint John Festival

    平面上n个红点,m个黑点,问你多少个黑点至少在一个红三角形内. 对红点求凸包后,转化为询问有多少个黑点在凸包内. 点在凸多边形内部判定,选定一个凸包上的点作原点,对凸包三角剖分,将其他的点极角排序之后 ...

  5. [SimpleOJ239]Cards

    题目大意: 有n(n为偶数)张牌,每张牌正反面有两张数字,你可以从中选出n/2张牌,减去某一面的数字,再选出另外n/2张牌,加上某一面的数字,问最终的答案最小能是多少? 思路: 先不考虑n/2的限制, ...

  6. 设置Eclipse编码方式

    1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到 general->Workspace,右侧Text file encodin ...

  7. 浅析HashMap和Hashtable的区别

    HashMap和Hashtable两个类都实现了Map接口,二者保存键值对(key-value对): HashMap和HashTable区别 第一,继承的父类不同.HashMap继承自Abstract ...

  8. opengl笔记——OpenGL好资料备忘

    Plane Equation 注:面可理解为:连接面上的点与原点,投影相同(为:a*x1+b*x2+c*x3) OpenGL Matrix Class (C++) Overview OpenGL fi ...

  9. 为DbContextScope添加数据库事务提交完成事件

    使用EF开发应用程序的一个难点就在于对其DbContext的生命周期管理,你的管理策略是否能很好的支持上层服务 使用独立事务,使用嵌套事务,并行执行,异步执行等需求? Mehdi El Gueddar ...

  10. java多线程处理导入数据拆分List集合 同步处理插入数据

    原文:https://www.2cto.com/kf/201612/581174.html import org.apache.log4j.Logger; import org.apache.poi. ...