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)的更多相关文章

  1. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  2. Leetcode之回溯法专题-52. N皇后 II(N-Queens II)

    Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...

  3. Java实现 LeetCode 52 N皇后 II

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

  4. leetcode 51. N皇后 及 52.N皇后 II

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

  5. lintcode 中等题:N Queens II N皇后问题 II

    题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...

  6. 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 ...

  7. 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. ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 神奇的null和undefined

    在JavaScript中,有两个特殊的类型存在,它们都只有一个值,分别null和undefined,之所以将它们放在一块,是因为在使用方面它们有很多相似之处. 相同点 在JavaScript中,nul ...

  2. java实验四——找鞍点

    package hello; public class 实验四 { public static void main(String[] args) { // TODO Auto-generated me ...

  3. 大家都对vertical-align的各说各话

    原文地址:http://www.blueidea.com/tech/web/2008/5892.asp 最近几天仔细研究了一下vertical-align这个属性,结果让我大吃一惊,这个很“资深”的C ...

  4. Linux命令详解-Apache网站服务器配置和管理

    1.Apache网站服务器配置和管理 1.源码包安装 2.rpm包安装 rpm –a | grep httpd 3.启动服务 service httpd start 4.配置文件: /etc/http ...

  5. Protocol Buffer Basics: Python

    原文https://developers.google.com/protocol-buffers/docs/pythontutorial Protocol Buffer Basics: Python ...

  6. Bind2nd源码解析

    例:transform(coll1.begin(), coll1.end(), back_inserter(coll2), bind2nd(multiplies<int>(), 10)); ...

  7. CGLib缺少jar出现 java.lang.ClassNotFoundException: org.objectweb.asm.Type

    CGLib实现动态代理区别于JDK动态代理,不需要目标类实现任何接口,是通过生成代理类子类的方式,而且据说速度要快于JDK动态代理.所以我想要试验一下CGlib的动态代理,网上找了些例子,自己动手写了 ...

  8. Hive基础之Hive是什么以及使用场景

    Hive是什么1)Hive由facebook开源,构建在Hadoop (HDFS/MR)上的用于管理和查询结果化/非结构化的数据仓库:2)一种可以存储.查询和分析存储在Hadoop 中的大规模数据的机 ...

  9. Java网络编程详解

    内容: 1.网络通信协议 2.UDP与TCP 3.UDP通信 4.TCP通信 5.网络编程总结 1.网络通信协议 (1)基本概念 网络:由多台计算机以及外部设备连接起来的一个系统,我们称之为网络 通信 ...

  10. swagger配置

    1.pom.xml <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <a ...