题目

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

分析

N皇后问题,同LeetCode 51 N-Queens,只不过,此题要求给出问题的合理解个数,而无需给出具体分布。

只需在上题的基础上,稍加修改,只计数便可达到要求,此题采用另一种求解N皇后问题的方法:

用一个一位数组来存放当前皇后的状态。假设数组为int state[n], state[i]表示第 i 行皇后所在的列。那么在新的一行 k 放置一个皇后后:

  1. 判断列是否冲突,只需要看state数组中state[0…k-1] 是否有和state[k]相等;

  2. 判断对角线是否冲突:如果两个皇后在同一对角线,那么|row1-row2| = |column1 - column2|,(row1,column1),(row2,column2)分别为冲突的两个皇后的位置

此种判别方式,相比上题采用的方法,简单许多。

AC代码

class Solution {
private:
int ret = 0;
public:
int totalNQueens(int n) {
if (n <= 0)
return 0; //存储安置皇后的当前解(存储N皇后所在的列数,初始化为-1)
vector<int> state(n, -1);
set_queens(state, 0);
return ret;
} void set_queens(vector<int> &state, int row)
{
int n = state.size();
if (row == n)
{
ret++;
return;
}
else{
for (int col = 0; col < n; col++)
{
if (isValid(state, row, col))
{
state[row] = col;
set_queens(state, row + 1);
state[row] = -1;
}//if
}//for
}
} //判断在row行col列位置放一个皇后,是否是合法的状态
//已经保证了每行一个皇后,只需要判断列是否合法以及对角线是否合法。
bool isValid(vector<int> &state, int row, int col)
{
for (int i = 0; i < row; i++)
{
if (state[i] == col || abs(row - i) == abs(col - state[i]))
return false;
}//for
return true;
}
};

GitHub测试程序源码

LeetCode(52) N-Queens II的更多相关文章

  1. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  2. LeetCode(90):子集 II

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

  3. LeetCode(219) Contains Duplicate II

    题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...

  4. LeetCode(52):N皇后 II

    Hard! 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方 ...

  5. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  6. LeetCode (45) Jump Game II

    题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...

  7. LeetCode(47):全排列 II

    Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...

  8. LeetCode(40) Combination Sum II

    题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...

  9. LeetCode(63)Unique Paths II

    题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...

随机推荐

  1. macOS 将【允许从以下位置下载的应用】设置为:任意来源

    用管理员帐号进入Terminal: 1) 输入:sudo spctl --master-disable ,回车: 2) 重新进入该设置页面即可看到已生效:

  2. Oracle 查询约束信息

    select * from user_constraints where table_name=''; select * from user_cons_columns;

  3. Web | 解决中文乱码

    设定文件的编码格式在head中添加 <head> <meta http-equiv="Content-Type" content="text/html; ...

  4. 解决 Cordova命令突然无法使用问题.

    问题背景 之前一直在做 Cordova 方面, 然后准备自己尝试使用 Vue + WebPack 再配合 Cordova 做一个 App . 更新了 npm , 然后然后, 我的 cordova 这个 ...

  5. magento layout xml 小结

    基础概念: http://magebase.com/magento-tutorials/demystifying-magentos-layout-xml-part-1/ 调试方案函数: $this-& ...

  6. CGI和Servlet的比较

    转载自:http://www.maxhis.info/java/cgi-vs-servlet/ 概括来说,CGI和Servlet可以完成相同的功能. CGI(Common Gateway Interf ...

  7. 域名IP主动验证(一)

    功能:主动验证给定的域名.IP对是否真正的关联 思路: 1.一开始通过修改hosts文件,把待验证的域名.IP对添加到文件里,然后用wget尝试访问,再恢复hosts文件重新验证下一对 2.后来了解到 ...

  8. iOS 自己手动添加编译警告

    文/青花瓷的平方(简书作者)原文链接:http://www.jianshu.com/p/b2e30cad2a0d著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 缘由 上一次生产环境我们 ...

  9. Thrift入门及Java实例演示【转】

    概述 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++.Java.Python.PHP.Ruby.Erlang.Perl.Ha ...

  10. jdbc获取数据具体过程

    下面是个最简单的使用jdbc取得数据的应用.在例子之后我将分成4步,分别是①取得连接,②创建PreparedStatement,③设置参数,④执行查询,来分步分析这个过程.除了设置参数那一步之外,其他 ...