[Leetcode] n queens ii n皇后问题
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

题意:仅要求给出解的种数。
思路:这题是n queens的简化版。思路是一样的,可参见上题的解法。代码如下:
class Solution
{
public:
int totalNQueens(int n)
{
int resNum=;
vector<int> state(n,-);
helper(state,,resNum);
return resNum;
} void helper(vector<int> &state,int row;int &resNum)
{
int n=state.size();
if(row==n)
{
resNum++;
}
else
{
for(int col=;col<n;++col)
{
if(isVaild(state,row,col))
{
state[row]=col;
helper(state,row+,resNum);
state[row]=-;
}
}
}
} bool isVaild(vector<int> &state,int row,int col)
{
for(int i=;i<row;++i)
{
if(state[i]==col||abs(row-i)==abs(col-state[i]))
retrun false;
}
return true;
}
};
注:还可以使用位运算解决n皇后的问题。
[Leetcode] n queens ii n皇后问题的更多相关文章
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- [LeetCode] 52. N-Queens II N皇后问题之二
The n-queens puzzle is the problem of placing nqueens on an n×n chessboard such that no two queens a ...
- [LeetCode] 52. N-Queens II N皇后问题 II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- [leetcode]52. N-Queens II N皇后
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- [LeetCode] Palindrome Partitioning II 解题笔记
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- LeetCode:课程表II【210】
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:子集 II【90】
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
随机推荐
- java后台去除JSON数组的重复值
假设原始Json数组是这样的 原始JSONArry:[{"Value":"15153129877","Key":"09770985 ...
- [Cracking the Coding Interview] 4.2 Minimal Tree 最小树
Given a sorted(increasing order) array with unique integer elements, write an algorithm to create a ...
- 查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
SELECT L.C# As 课程ID,L.score AS 最高分,R.score AS 最低分 FROM SC L ,SC AS R WHERE L.C# = R.C# and L.score = ...
- angularjs post data
//post json 时收不到数据,目前只找到方法post form形式的key-value值 //关键是设置 headers: { 'Content-Type': 'application/x- ...
- Jersey2+swagger组建restful风格api及文档管理
1.jar包引入 <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId& ...
- Git使用之二:下载远程代码到本地指定文件夹
一.前期工作: 1.准备好本地的文件夹 2.如果后期需要继续以该文件夹进行同步的,则需要配置该文件夹,方法请参考之前的 Git使用之一:创建仓储和提交文件 二.用clone(克隆方式下载) 在本地下 ...
- 2007: [Noi2010]海拔
2007: [Noi2010]海拔 https://www.lydsy.com/JudgeOnline/problem.php?id=2007 分析: 平面图最小割. S在左下,T在右上,从S到T的一 ...
- 3155: Preprefix sum
3155: Preprefix sum https://www.lydsy.com/JudgeOnline/problem.php?id=3155 分析: 区间修改,区间查询,线段树就好了. 然后,这 ...
- 0301001_Lesson1&2
Lesson 1 Excuse me! 对不起! Listen to the tape then answer this question.Whose handbag is it?听录音,然后回答问题 ...
- Linux下启动Oracle服务和监听程序步骤
Linux下启动Oracle服务和监听程序启动和关闭步骤整理如下: 1.安装oracle: 2.创建oracle系统用户: 3./home/oracle下面的.bash_profile添加几个环境变量 ...