LeetCode(52) N-Queens II
题目
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 放置一个皇后后:
判断列是否冲突,只需要看state数组中state[0…k-1] 是否有和state[k]相等;
判断对角线是否冲突:如果两个皇后在同一对角线,那么|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;
}
};
LeetCode(52) N-Queens II的更多相关文章
- 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 ...
- LeetCode(90):子集 II
Medium! 题目描述: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1 ...
- LeetCode(219) Contains Duplicate II
题目 Given an array of integers and an integer k, find out whether there are two distinct indices i an ...
- LeetCode(52):N皇后 II
Hard! 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方 ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode (45) Jump Game II
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- LeetCode(47):全排列 II
Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...
- LeetCode(40) Combination Sum II
题目 Given a collection of candidate numbers (C) and a target number (T), find all unique combinations ...
- LeetCode(63)Unique Paths II
题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...
随机推荐
- python三行代码实现快速排序
def quick_sort(array): if len(array) < 2: return array return quick_sort([lt for lt in array[1:] ...
- Codeforces Round #261 (Div. 2) C
Description Recently Pashmak has been employed in a transportation company. The company has kbuses a ...
- 2018 ACM-ICPC亚洲区域赛(青岛)
Problem C---zoj 4060 Flippy Sequence 解题思路:要求进行两次操作,每次操作选择一个区间,问将s串变成t串中所选的两个区间构成的4元组有多少个.做法:找出s串与t串不 ...
- 什么是Servlet容器?(分析很到位)
在本文中,我写了一些关于Web服务器.Servlet容器以及它与JVM的关系的基本概念.我想表达的是,Servlet容器也仅仅不过是一个Java程序. 1. 什么是Web服务器? 想要知道什么是Ser ...
- Setting up IPS/inline for Linux in Suricata
不多说,直接上干货! 见官网 https://suricata.readthedocs.io/en/latest/setting-up-ipsinline-for-linux.html Docs » ...
- Suricata的规则解读(默认和自定义)
不多说,直接上干货! 见suricata官网 https://suricata.readthedocs.io/en/latest/rules/index.html 一.Suricata的规则所放位置 ...
- 数位dp知识
转自http://blog.csdn.net/zhaoxinfan/article/details/8707605 下面先给出数位DP的背景: •在给定区间[A,B]内,找满足要求的数. •要求一般和 ...
- [转]VC中调用外部exe程序方式
本文转自:http://blog.sina.com.cn/s/blog_486285690100ljwu.html 目前知道三种方式:WinExec,ShellExecute ,CreateProce ...
- 如何使用 Java 生成二维码
步骤 下载jar包(QRCode.jar) maven项目手动引入jar包 编写实体类实现二维码的生成 controller调用 下载jar包(QRCode.jar) 下载网址如下: QRCode生成 ...
- JSP报错The value for the useBean class attribute *** is invalid.
环境:IDEA+Tomcat9+JDK1.8 在前期学习时,环境一直能够"正常"使用,实际上环境并没有完全搭建成功. 推荐: https://blog.csdn.net/lw_po ...