52. N-Queens II (Array; Back-Track)
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

class Solution {
public:
int totalNQueens(int n) {
result = ;
if(n==) return result;
vector<int> flag(n,-); //每行的Q出现在哪列
backTracking(n, flag, );
return result;
}
void backTracking(int n, vector<int>& flag, int depth){ //depth is the line number
if(depth==n){
result++;
return;
}
for(int i = ; i < n; i++){ //traverse column
if(check(n,flag, depth, i)) {
flag[depth] = i;
backTracking(n,flag,depth+);
flag[depth] = -; // back track
}
}
}
bool check(int n, vector<int>& flag, int i, int j){
for(int k = ; k < i; k++){
if(flag[k] < ) continue;//no Q in this column
if(flag[k]==j) return false;//check columns
if(abs(i-k)==abs(j-flag[k])) return false; //check cross lines
}
return true;
}
private:
int result;
};
52. N-Queens II (Array; Back-Track)的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- Leetcode之回溯法专题-52. N皇后 II(N-Queens II)
Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- leetcode 51. N皇后 及 52.N皇后 II
51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- 122. Best Time to Buy and Sell Stock II (Array;Greedy)
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 154. Find Minimum in Rotated Sorted Array II (Array; Divide-and-Conquer)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- 81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 45. Jump Game II (Array; Two-Pointers,Greedy)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- JavaScript-Tool:Numeral.js
ylbtech-JavaScript-Tool:Numeral.js A javascript library for formatting and manipulating numbers. 1. ...
- PHP PDO prepare()、execute()和bindParam()方法详解
每次将查询发送给MySQL服务器时,都必须解析该查询的语法,确保结构正确并能够执行.这是这个过程中必要的步骤,但也确实带来了一些开销.做一次是必要的,但如果反复地执行相同的查询,批量插入多行并只改变列 ...
- 【Python编程:从入门到实践】chapter2 变量和简单数据类型
2.1 运行2.2 变量 message = "hello" print(message) 2.2.1 变量的命名和使用 2.2.2 使用变量是避免命名错误2.3 字符串 “Hel ...
- setTimeout 方法带参数传递
setTimeout(callback, after, arg1, arg2); 其中,callback即function(){},after为时间参数,指多久后执行callback,单位为毫秒,30 ...
- nginx, supervisor
Nginx(单进程): 反向代理, 负载均衡.图解 将配置文件 nginx.conf 的 user xx 配置好 xx用户 检查语法 $ sudo service nginx configtest 重 ...
- Mysql-表关系
表关系分为三种:一对一,一对多,多对多 一对多:一个学院对应多个学生,而一个学生只对应一个学院 -- 这儿classroom 是代表的学院. -- 一对多 - A表的一条记录 对应 B 表多条记 ...
- hbase 简介
hbase是hadoop的数据库,能够对大数据提供随机实时的读写访问功能,具有开源,分布式,可扩展行,面向列存储的特点.hbase的目标是存储并处理大量的数据. 它可以直接使用本地文件系统,也可以使用 ...
- idea 安装 破解方法
参考:https://blog.csdn.net/qq_27686779/article/details/78870816 (1)下载破解补丁 把下载的破解补丁放在你的idea的安装目录下的bin的目 ...
- JAVA Spring 简单的配置和操作 ( 创建实体类, 配置XML文件, 调试 )
< 1 > 实体类 Person package java_spring.modle; /** * 一个实体类( Person ) */ public class Person { pri ...
- leetcode242
public class Solution { public bool IsAnagram(string s, string t) { Dictionary<char, int> dic ...