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 ...
随机推荐
- 2.tensorflow——Softmax回归
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow.examples. ...
- Windows平台下在Emacs中使用plantuml中文乱码问题(已解决)
Windows平台下在Emacs中使用plantuml中文乱码问题(已解决) */--> code {color: #FF0000} pre.src {background-color: #00 ...
- uWSGI、WSGI、uwsgi、wsgiref、werkzeug
WSGI WSGI:全称是Web Server Gateway Interface,WSGI不是服务器,也不是python模块.框架.API或者任何软件,只是一种规范,描述web server如何与w ...
- java虚拟机规范(se8)——class文件格式(三)
4.5 字段 字段使用field_info结构来描述. 在同一个class文件中的两个字段不能有相同的名称和描述符. 结构的格式如下: field_info { u2 access_flags; u2 ...
- Elasticsearch学习,请先看这一篇
题记: Elasticsearch研究有一段时间了,现特将Elasticsearch相关核心知识.原理从初学者认知.学习的角度,从以下9个方面进行详细梳理.欢迎讨论-- 0. 带着问题上路--ES是如 ...
- kubernetes里面有时候centos源用不了
kubernetes里面有时候centos源用不了,快速配一个阿里云的源. mkdir /etc/yum.repos.d/yangback;mv /etc/yum.repos.d/* /etc/yum ...
- jsonp详细原理之一
/*script标签是不存在跨域请求的,类似的还有img,background:url,link 你可以想象一下,平时的这些标签都是可以直接引入外部资源的,所以是不存在跨域问题的*/ function ...
- Ansible--01 ansible基础 Ansible-ad- hoc
目录 自动化运维工具-Ansible基础 自动化运维的含义 Ansible 基础及安装 Ansible的架构 Ansible的执行流程 ansible配置文件 ansible Inventory(主机 ...
- logback-paycore.xml 日志配置
<?xml version="1.0" encoding="UTF-8"?><configuration> <property n ...
- Es学习第二课, ES安装和客户端使用
Elasticsearch安装依赖于jdk,所以大家先保证自己电脑安装好Java环境(JDK7或更高版本),并配置好环境变量:这块的配置我就不细讲了,不管是Linux还是Windows,这块的安装和配 ...