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皇后问题的更多相关文章

  1. lintcode 中等题:N Queens II N皇后问题 II

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

  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 ...

  3. [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 ...

  4. [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 ...

  5. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  6. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  7. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  8. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  9. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...

随机推荐

  1. c# WebBrowser开发参考资料--杂七杂八

    c# WebBrowser开发参考资料 http://hi.baidu.com/motiansen/blog/item/9e99a518233ca3b24aedbca9.html=========== ...

  2. C语言Windows程序开发—CreateWindow函数介绍【第03天】

    (一)CreateWindow函数的参数介绍: HWND CreateWindow( LPCTSTR lpClassName, //Windows窗口中预定义的控件结构体,包括:BUTTON(按钮), ...

  3. Android面试收集录 电话、短信和联系人、多媒体技术

    1.请写出调用系统拨号界面? Intent intent=new Intent(Intent.ACTION_DIAL,Uri.pase("tel:12345678910")); s ...

  4. 从装机到配置-CentOS6.5

    L006课程结束后的总结 首先:系统(cat /etc/redhat-release):CentOS release 6.5 (Final) 版本(uname -r):2.6.32-431.el6.x ...

  5. SharePoint显示错误信息

         在SharePoint项目中,一般如果发生错误,SharePoint会弹出它自定义的报错页面,一般就显示"Something went wrong",如果光是看这一句话, ...

  6. C#文件重命名的代码

    C#中没有重命名的方法,自己写了一个方法,来处理文件的重命名. /// <summary> /// 重命名文件夹内的所有子文件夹 /// </summary> /// < ...

  7. 关于ArrayList add()方法 中的引用问题

    ArrayList的add方法每次添加一个对象时,添加 的是一个对象的引用,比如进行循环操作10次  lists.add(a) 每次 a会改变 ,这时候你会发现你在lists里添加了10个相同的对象a ...

  8. mysql 处理日期格式

    DATE_FORMAT(createTime,'%Y-%m-%d %H:%i:%s') 对应格式: 2018-12-17 17:33:43 DATE_FORMAT()函数所有格式:   以后有需要在自 ...

  9. hdu1506 Largest Rectangle in a Histogram

    Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...

  10. 问题 A: Least Common Multiple

    题目描述 The least common multiple (LCM) of a set of positive integers is the smallest positive integer ...