题目

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. PHP + jquery.validate remote的用法

    [ 转 ] http://www.cnlvzi.com/index.php/Index/article/id/58 最近做验证功能时,用到jquery.validate.js中的remote远程验证方 ...

  2. css - 单词的自动换行问题

    转载自:解决文档中有url链接时被强制换行的问题 问题 当行内出现很长的英文单词或者url的时候,会出现自动换行的问题,为了美化页面,往往会希望这些很长的英文单词或者url能够断开来,超出的部分换行到 ...

  3. Centos6.8 搭建 Mysql 主从复制

    实例环境: MySQL-Master:Centos-6.8:192.168.153.130 MySQL-Slave:Centos-6.8:192.168.153.131 1.两台服务器安装mysql ...

  4. IIR型高斯滤波的原理及实现

    二.实现 GIMP中有IIR型高斯滤波的实现,代码位于contrast-retinex.c中,读者可自行查看.下面给出本人实现的核心代码: #include"stdafx.h" t ...

  5. POJ 1692 Crossed Matchings dp[][] 比较有意思的dp

    http://poj.org/problem?id=1692 这题看完题后就觉得我肯定不会的了,但是题解却很好理解.- - ,做题阴影吗 所以我还是需要多思考. 题目是给定两个数组,要求找出最大匹配数 ...

  6. JVM 内存机制理解【转自http://www.cnblogs.com/dingyingsi/p/3760447.html】

    我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互,而CPU运转速度越来越快,磁盘远远跟不上CPU的读写速度,才设计了内存,用户缓冲用户IO等待导致CPU的等 ...

  7. Android提供的对话框

    1.普通对话框: 给出提示信息,有yes.no两个按钮. AlertDialog dialog=new AlertDialog.Builder(this) //this代表当前Activity对象,表 ...

  8. 作用域链、this细说

    一.作用域链 作用域:浏览器给js的一个生存环境(栈内存) 作用域链:js中的关键字var和function 都可以提前声明和定义,提前声明和定义的放在我们的内存地址(堆内存)中.然后js从上到下逐行 ...

  9. 【学习笔记】block、inline(替换元素、不可替换元素)、inline-block的理解

    本文转载 总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) 和 inline elements (内联元素).blo ...

  10. 安装nginx的一些注意事项

    1.如何彻底屏蔽掉Nginx的banner 为了安全或者某些个人的原因,如果要屏蔽掉nginx的banner,要修改以下几个位置: src/http/ngx_http_header_filter_mo ...