题目

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. django相关命令

    1 安装django pip3 install django 2 django-admin命令 django-admin startproject mysite #创建一个项目 3 manage.py ...

  2. Social Net ZOJ - 3649

    Social Net ZOJ - 3649 题意: 反正原题题意我是看不懂... 参考:http://www.cnblogs.com/names-yc/p/4922867.html 给出一幅图,求最大 ...

  3. Oracle apex 搭建

    参考文档 : 百度文库  <Oracle ERP APEX开发指南 > APEX HOME:  /oracle11g/product/11.2/apex/apex Oracle home: ...

  4. web 另类方法实现“另存为”功能

    HTML5 的 FileReader 带给我们很强大的文件只读访问能力,可是在 HTML5 新增的 JS 中却没有发现有方便的另存到本地文件的相关 API,以往的办法要么调用浏览器的 save as ...

  5. Python 3.6.5安装过程中小错误zipimport.ZipImportError: can't decompress data; zlib not available

    执行 :yum install -y zlib*之后,就好了.该安装错误是在CentOS7.4中遇到的.

  6. webpack采坑十连跳

    4.css.js一同打包进html ---------------- https://www.cnblogs.com/amiezhang/p/9723565.html 依赖内联插件  HtmlWebp ...

  7. 洛谷P2742 【模板】二维凸包

    题意 求凸包 Sol Andrew算法: 首先按照$x$为第一关键字,$y$为第二关键字从小到大排序,并删除重复的点 用栈维护凸包内的点 1.把$p_1, p_2$放入栈中 2.若$p_{i{(i & ...

  8. 伟景行 citymaker 从入门到精通系列

    伟景行 citymaker 从入门到精通(1)——js开发,最基本demo,加载cep工程文件 伟景行 citymaker 从入门到精通(2)——工程图层树加载   伟景行 citymaker 从入门 ...

  9. QTableWidget表头样式

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7742680.html QTableView { background-color: rgba(255, 255 ...

  10. cnbeta新闻资讯第三方客户端应用

    该源码案例是一个cnbeta第三方客户端应用案例,作者ywwxhz,源码cnBeta-reader,cnbeta 的 Android 客户端项目. 源码下载: http://code.662p.com ...