LeetCode-NQueensII
经典的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的更多相关文章
- leetcode N-QueensII
题目和上一题一样,就是要求输出有多少种结果.最直接的就是,只要在上一题的代码return ans.size();就可以了.果然也是AC了. 然后我翻看了几种别人写的,暂时还没有找到复杂度可以比上一题降 ...
- Leetcode:52. N-QueensII
Description Follow up for N-Queens problem. Now, instead outputting board configurations, return the ...
- leetcode 98:n-queens-ii
题目描述 继续思考"n-queens"问题 这次我们不是输出皇后的排列情况,而是输出n皇后问题一共有多少种解法 Follow up for N-Queens problem. No ...
- [LeetCode] N-Queens II N皇后问题之二
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- [LeetCode]题解(python):052-N-Queens II
题目来源 https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead outputt ...
- <转>LeetCode 题目总结/分类
原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...
- [Leetcode][Python]52: N-Queens II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 52: N-Queens IIhttps://oj.leetcode.com/ ...
- N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法
回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...
随机推荐
- 安装 Apache 源代码包
把自己在网易博客的文章迁移过来 cd /lamp/httpd-2.2.9 ./configure --prefix=/usr/local/apache2/ --sysconfdir=/usr/loca ...
- CodeForces 732E Sockets
贪心,优先队列. 将$s$按照从小到大的顺序扔进优先队列.从小的开始与电脑配对,如果找不到合适的电脑,那么再变小一次,直到找到与之配对的电脑或者作废. #pragma comment(linker, ...
- Java HashSet的元素内容变化导致的问题
概述 HashSet元素引用的对象的内容发生变化,会导致"元素不属于集合"的问题.事实上这个元素还在集合里,但是调用contains方法进行判断,得到的结果却是false. 正文 ...
- 洛谷——P1862 输油管道问题
P1862 输油管道问题 题目背景 听说最近石油危机 所以想到了这题 题目描述 某石油公司计划建造一条由东向西的主要输油管道.该管道要穿过一个有n口油井的油田.从每口油井都要有一条输油管道沿最短路径( ...
- 北大青鸟代码---asp.net初学者宝典
一.上传图片:使用控件:file,button,image; 上传按钮的代码: string fullfilename=this.File1 .PostedFile .FileName ;取得本地文件 ...
- 初雪-Diary?
who care ------------2018 11 6-------------- 终于AK一场啦 ------------2018 10 18-------------- 嗯....今天T2多 ...
- CodeForces - 1000E We Need More Bosses
题面在这里! 依然一眼题,求出割边之后把图缩成一棵树,然后直接求最长链就行了2333 #include<bits/stdc++.h> #define ll long long using ...
- [Lydsy1806月赛] 质数拆分
(mmp我已经不知道是第几次写NTT被卡了) 可以发现质数个数是 N/log(N) 级别的,1.5*10^5之内也只有 10000 多一点质数. 所以我们第一层暴力卷积,常数可以优化成 1/2. 然后 ...
- 客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值。
客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值. 无论是什么的html控件,只要加上了runat="server" ...
- [转]ssis cannot retrieve the column code page info from the ole db provider
本文转自:http://social.msdn.microsoft.com/Forums/sqlserver/en-US/dc1a61f2-1ab8-4ed3-b85c-db6481800b50/er ...