【LeetCode】52. N-Queens II
N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

简单的回溯法。
cur[i] == j 代表第i行的皇后位于第j列。
对于每一行,尝试所有列,
如果当前行尝试的位置不与前面所有行的位置冲突,则可以递归到下一行。
class Solution {
public:
int totalNQueens(int n) {
int count = ;
vector<int> cur;
Helper(count, cur, , n);
return count;
}
void Helper(int& count, vector<int> cur, int pos, int n)
{
if(pos == n)
count ++;
else
{
for(int i = ; i < n; i ++)
{
cur.push_back(i);
if(check(cur))
Helper(count, cur, pos+, n);
cur.pop_back();
}
}
}
bool check(vector<int> cur)
{
int size = cur.size();
int loc = cur[size-];
for(int i = ; i < size-; i ++)
{
//never same row
//col
if(cur[i] == loc)
return false;
//diag
if(abs(cur[i]-loc) == abs(i-size+))
return false;
}
return true;
}
};

【LeetCode】52. N-Queens II的更多相关文章
- 【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 ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】52. N-Queens II(位运算)
[题意] 输出N皇后问题的解法个数. [题解] 解法一:传统dfs回溯,模拟Q放置的位置即可,应该不难,虽然能通过,但是时间复杂度很高. 解法二:位运算大法好! 首先要明白这道题里两个核心的位运算 1 ...
- 【LeetCode】Single Number I & II & III
Single Number I : Given an array of integers, every element appears twice except for one. Find that ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 【LeetCode】680. Valid Palindrome II
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...
随机推荐
- C++中对Mysql的操作函数可以参考以下blog中的内容
http://www.cnblogs.com/lovebread/archive/2009/11/24/1609936.html
- 第二章 Javac编译原理
注:本文主要记录自<深入分析java web技术内幕>"第四章 javac编译原理" 1.javac作用 将*.java源代码文件转化为*.class文件 2.编译流程 ...
- OkHttp 官方Wiki之【使用案例】
原文位置:https://github.com/square/okhttp/wiki/Recipes Recipes 食谱/知识点清单 We've written some recipes that ...
- Socket.IO介绍:支持WebSocket、用于WEB端的即时通讯的框架
一.基本介绍 WebSocket是HTML5的一种新通信协议,它实现了浏览器与服务器之间的双向通讯.而Socket.IO是一个完全由JavaScript实现.基于Node.js.支持WebSocket ...
- IOS之正则表达式
在现阶IOS开发的样式越来越多,我们在开发APP的时候难免会遇到对用户的登录和注册进行操作,但是登录注册如果想要做的人性化少不了的就是校验,对当前用户的登录信息进行校验,如果满足要求我们会把用户注册的 ...
- js数组对象深度复制
var deepCopy = function(o) { if (o instanceof Array) { var n = []; for (var i = 0; i < o.length; ...
- 【python】理想论坛爬虫长贴版1.00
理想论坛有些长贴,针对这些长贴做统计可以知道某ID什么时段更活跃. 爬虫代码为: #---------------------------------------------------------- ...
- #lspci | grep Eth
该命令作用:将lspci的输出当做输入,从中找出包含Eth的行.在我的Fedora机器上运行结果为 [root@localhost etc]# lspci | grep Eth00:04.0 Ethe ...
- rapidxml 解析修改内存的值
1.使用rapidxml解析的时候,也就是 调用xmlDoc.parse<0>(xmlContent),特别注意,rapidxml会修改内存的值,把右尖括号>修改为'\0',因此特别 ...
- STL - Map - 运行期自定义排序
RuntimeStringCmp.cpp #include <string> using namespace std; // function object to compare stri ...