题目链接: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. Java学习之多线程(定义)

    进程:正在运行中的程序线程:负责执行程序的控制单元(执行路径)一个进程中可以有多个执行路径,称之为多线程一个进程中至少要有一个线程 创建新执行线程有两种方式 一.继承Thread类步骤:1.定义一个类 ...

  2. maven(二),Linux安装maven3.5.3及配置

    Linux系统,ubuntu-16.04.4,安装maven3.5.3 一.创建文件夹 注意Linux用户,这个如果不是root用户,命令前面需要加:sudo //创建一个目录 mkdir /usr/ ...

  3. 用swith语句来键入一个整数输出对应是星期几

    基本格式:switch(表达式) { //基本数据类型可以接收byte,short,char,int 引用数据类型可以接收枚举(JDK1.5)String字符串(JDK1.7) case 值1: 语句 ...

  4. Scrapy框架: 通用爬虫之XMLFeedSpider

    步骤01: 创建项目 scrapy startproject xmlfeedspider 步骤02: 使用XMLFeedSpider模版创建爬虫 scrapy genspider -t xmlfeed ...

  5. 使用腾讯地图请求来源未被授权, 此次请求来源域名/ip:servicewechat.com

    原文:微信小程序使用腾讯地图请求来源未被授权, 此次请求来源域名:servicewechat.com 版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明 ...

  6. go读写文本文件

    一.文件读取 1. 将整个文件读取到内存中 package main import ( "flag" "fmt" "io/ioutil" ) ...

  7. Java中的动态代理(jdk和cglib)

    JAVA的动态代理 代理模式 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类 ...

  8. docker的入门简介

    可能写的不是很完美,需要大家指正修改和意见(谢谢合作) docker的入门: docker的好处: 1.更快交付你的应用(Faster delivery of your applications) 2 ...

  9. React-Native初识-安卓篇(一)

    前言:React-Native简称RN,可以用来构建Android和IOS的应用程序,在接下来的两个半月里,我会记录下本人在学习RN开发项目中的点滴. 本篇目录: 1.React-Native初识 2 ...

  10. CSS实现不换行/自动换行/文本超出隐藏显示省略号

    在写页面的时候,我们经常会需要用到关于文本的换行,强制换行以及显示几行超过显示省略号等,今天我们就对这些问题来做个汇总吧! 1.自动换行 div{ word-wrap:break-word; word ...