经典的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. Vue 2.0 Application Sample

    ===搭建Demo=== http://blog.csdn.net/wangjiaohome/article/details/51728217 ===单页Application=== http://b ...

  2. Flask实战第60天:帖子分页技术实现

    编辑manage.py,添加测试帖子 @manager.command def create_test_post(): for x in range(1, 100): title = '标题{}'.f ...

  3. NGUI EventDelagate事件委托

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class BUttonCl ...

  4. 【JavaScript】JS将Java的Timestamp转为Date类型

    遇到一个小需求,由于要填充日期插件里的数据,前台要把java后台传来的Date类型的数据转成YYYY-MM-DD格式的时间数据.通过json传输,Java的Date类型的数据自动转成了时间戳,例如 “ ...

  5. sql 常见错误

    notFound = 1403L; .dupKey = -1L; openCloseErr = -2117L; cursorNotOpenErr = -1002L; .nullCursor = -14 ...

  6. Xamarin中VS无法连接Mac系统的解决办法

    Xamarin中VS无法连接Mac系统的解决办法 按照以下步骤排查:(1)确认Mac系统中安装Xamarin.iOS开发必备的组件,如Mono.Xamarin.iOS.(2)将Windows和Mac下 ...

  7. VB查询数据库之结账——机房收费系统总结(五)

    对于机房收费的结账,我感觉是所有窗体中,最难的一个.这个窗体我真的做了好多天.它的难度系数我感觉是最高的. 首先,你要理清上机时间和收费标准的关系,在预备时间中,是不收费的. 其次,在超过预备时间,一 ...

  8. 【BZOJ 4572】【SCOI 2016】围棋

    http://www.lydsy.com/JudgeOnline/problem.php?id=4572 轮廓线DP:设\(f(i,j,S,x,y)\). \(S\)表示\((i,1)\)到\((i, ...

  9. 【模拟退火】poj2069 Super Star

    题意:让你求空间内n个点的最小覆盖球. 模拟退火随机走的时候主要有这几种走法:①随机旋转角度. ②直接不随机,往最远的点的方向走,仅仅在尝试接受解的时候用概率.(最小圆/球覆盖时常用) ③往所有点的方 ...

  10. Spring+Spring MVC+MyBatis整合

    一.准备工作    1.1导入所需jar包 1.2数据库 CREATE TABLE `t_customer` ( `id` ) NOT NULL AUTO_INCREMENT, `username` ...