LeetCode N皇后 & N皇后 II
题目链接: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的更多相关文章
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- Leetcode:面试题68 - II. 二叉树的最近公共祖先
Leetcode:面试题68 - II. 二叉树的最近公共祖先 Leetcode:面试题68 - II. 二叉树的最近公共祖先 Talk is cheap . Show me the code . / ...
- Leetcode:面试题55 - II. 平衡二叉树
Leetcode:面试题55 - II. 平衡二叉树 Leetcode:面试题55 - II. 平衡二叉树 Talk is cheap . Show me the code . /** * Defin ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【LeetCode】731. My Calendar II 解题报告(Python)
[LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
随机推荐
- 测开之路五十五:实现类似于unittest查找case
实现给一个路径,去查找test开头的测试用例文件 创建一个计算器的类,方便后面测试用 class Calculator(object): def add(self, x, y): return x + ...
- UITableView 支持左右滑动(一)
原理如下: SwipeTableView subView 1 : UIScrollView作为容器, 主要负责左右滑动, 每个tableView的顶部设置相同的contentInset subVie ...
- Java学习之线程间通信(双线程)
线程间通讯:多个线程在处理同一资源,但是任务不同 练习一:双线程出现线程安全问题,需要使用同步,思考同步代码添加位置需求:银行账户存钱,显示谁在账户存钱了,存了多少钱分析:操作同一银行账户两个不同的操 ...
- OpenCV2.4.8 + CUDA7.5 + VS2013 配置
配置过程主要参考:https://initialneil.wordpress.com/2014/09/25/opencv-2-4-9-cuda-6-5-visual-studio-2013/ 1.为什 ...
- shell查词典
curl http://cn.bing.com/dict/search?q=spawn -s | sed -e '{s/<\/span>/&\n/g}' | sed -n '{/& ...
- VUX中selector组件数据错误,value-map不能正常转换接口数据
项目中有个地方需要用到下拉框,使用VUX的selector组件,使用value-map属性进行接口数据转换未成功,出来的还是原数据 看了又看也没写错呀,字段什么的都是复制上去的,去网上查了也没查到怎么 ...
- python 自动把mysql备份文件发送邮箱
import os import time import sched import smtplib from email.mime.text import MIMEText from email.he ...
- kubernetes容器集群管理创建node节点kubeconfig文件
1.创建TLS Bootstrapping Token 2.创建kubelet kubeconfig 3.创建kube-proxy kubeconfig 安装和设置kubectl [root@mast ...
- Web前端基础学习-3
bfc(block formatting context) 块级格式化上下文 生成bfc的方式: 1.根元素: 2.float属性不为none(脱离文档流): 3.position为absolute或 ...
- Django版本更新(升级)到指定版本的命令