经典的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. firfox与about:config

    ¤什么是about:config¤about:config是Firefox的设置页面,Firefox提供了不少高级设置选项在这里以便让你可以更加详细地控制Firefox的运行方式.官方不推荐用户手工修 ...

  2. 初始pip

    关于pip包括下面的东西还不是很懂,慢慢的了解,我的pip是从https://bootstrap.pypa.io/get-pip.py 粘贴并命名为 get-pip.py 后,执行 python ge ...

  3. VisualStudio 2013 快捷键

    有些快捷键不是默认的,需要插件支持,如Resharper,WebEssentials,VSCommands Ctrl单键系列 Ctrl+Q Quick Info Ctrl+W Extend Selec ...

  4. Java实现打包下载BLOB字段中的文件

    概述 web项目的文件打包下载实现:servlet接收请求,spring工具类访问数据库及简化大字段内容获取,org.apache.tools.zip打包. 必要提醒:当前总结是继Java实现下载BL ...

  5. IRC程序学习

    %%聊天的中转站,将{chan,MM,Msg}形式的信息转化为 {mm, MM, Msg}形式 -module(mod_chat_controller). -export([start/3]). -i ...

  6. NOI 二分算法练习

    1.NOI 二分法求函数的零点 总时间限制:  1000ms 内存限制:  65536kB 描述 有函数: f(x) = x5 - 15 * x4+ 85 * x3- 225 * x2+ 274 * ...

  7. Smart config风险分析与对策

    Smart config风险分析与对策 1.简介:          Smart config是一种将未联网设备快速连接wifi的技术,大概原理如下图所示: 2.业务需求:          要求实现 ...

  8. In c++ access control works on per-class basis not on per-object basis.

    #ifndef MYTIME_H #define MYTIME_H class MyTime { private: int m_hour; int m_minute; public: MyTime() ...

  9. 前端工业化工具Grunt初体验

    今天来学学Grunt~~目的是为了自动化!自动压缩...自动修复...自动合并等... 提示:Grunt基于Node.js,安装之前要先安装Node.js 1.安装 grunt-cli npm ins ...

  10. python2和python3输入输出相关

    python3: #coding=utf-8 a = input("请输入你的名字:") print("%s"%a) #输出没有一点问题,a就是字符串(或者数字 ...