题目链接: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. USACO 5.3 章节

    相关讲解可在USACO上看原文,也可以搜索nocow找到翻译的! (nocow上有些微翻译是有问题的,如果想看nocow翻译的建议也对着英文看) 以下记录以下 自己之前未掌握的一些要点,以及按自己的括 ...

  2. 搜索的应用--计算最优解:Aizu - ALDS1_4_D Allocation

    搜索的应用-计算最优解 题目: You are given nn packages of wiwi kg from a belt conveyor in order (i=0,1,...n−1i=0, ...

  3. QTP 通过URL地址下载文件到本地(转)

    While automation, you may come to situations where you need to need to download a file on clicking a ...

  4. web自动化,selenium 无法清空输入框默认值继续输入

    有的页面输入框自带默认值,想要修改里面的内容时,先使用clear()再send_keys(),这种方式无法清除只会在默认值后面追加内容,不是我想要的结果 解决方法: 方法一: 先双击,后直接send_ ...

  5. htmlspecialchars_decode 解决掉 &amp;

    如果在请求中返回的内容包含 & 请使用htmlspecialchars_decode 搞一下,去掉. 这个纯粹为自己怕到时又找不到这个方法

  6. HDU 1709 The Balance( DP )

    The Balance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. Swift——(六)Swift中的值类型

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/twlkyao/article/details/34855597     在Swift中,结构体和枚举 ...

  8. static的变量是放在哪里

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

  9. elasticsearch Java Client用户指南

    这里使用的Java客户端版本是5.1.2,Elasticsearch的版本号也要是5.1.2,否则一些功能可能不支持. 之前介绍过Spring Data Elasticsearch,那里也是使用了本文 ...

  10. Java高频经典面试题(第一季)四:方法的参数传递机制

    考点? 方法的参数传递机制 String,包装类等对象的不可变性 方法的参数传递机制: ①形参是基本数据类型 传递数据值 ②实参是引用数据类型 传递地址值 特殊的类型:String.包装类等对象不可变 ...