[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] 输出: ...
随机推荐
- MySQL实现排名并查询指定用户排名功能,并列排名功能
MySQL实现排名并查询指定用户排名功能,并列排名功能 表结构: CREATE TABLE test.testsort ( id int(11) NOT NULL AUTO_INCREMENT, ui ...
- Xshell4 出现Linux中中文字符乱码问题
Xshell5竟然收费了... 没办法只能用回Xshell4 但是不知道是版本不对还是在咋的 发现中文乱码,导致操作非常不方便 解决方案 LANG=zh_CN.big5 执行在终端执行上面的命令就可以 ...
- web学习第一天
学习web心得 表格 table,表单 form,跑马灯效果 marquee,导入背景图片<img src="图片路径"> 第一天学的不是很难,作业也相对来说比较简单, ...
- python中一些内置函数实例
lambda表达式 简单函数可用lambda表达式 1. def f1() return(123) r1=f1() print() 2. f2=lambda:123 r2=f2() print() 以 ...
- linux网络服务实验
1.设置window IP地址为192.168.3.XX,掩码24位. 2.设置Linux IP地址为192.168.3.YY,掩码24位.window与Linux互相ping通. 3.在linux中 ...
- go学习笔记-语言基础
语言基础 结构 基础组成: 包声明 引入包 函数 变量 语句 & 表达式 注释 程序 在开始编写应用之前,我们先从最基本的程序开始,在学习大部分语言之前,都会编写一个可以输出hello wor ...
- js倒计时页面跳转
HTML: <p><span id="timer">60</span>s 后跳转到百度首页</p> JS: //倒计时方法 func ...
- jmeter更改启动编码设置
项目中碰到这样的问题,在eclipse经过utf-8转码的代码,能正常运行,放到了jmeter里面运行,就是乱码,如下: String s = "乔佳飞"; String ss = ...
- jenkins使用Role Strategy管理用户权限
下载插件地址:https://wiki.jenkins.io/display/JENKINS/Role+Strategy+Plugin 1. 安装好插件后,进入jenkins系统管理的Configur ...
- springmvc常用jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans ...