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. OpenGL 画出雷达动态扫描效果(一)

    最终效果如下所示 Demo下载  http://files.cnblogs.com/xd-jinjian/Debug.zip 源代码下载 http://download.csdn.net/detail ...

  2. 2018ICPC网络赛(徐州站)A题题解

    一.题目链接 https://nanti.jisuanke.com/t/31453 二.题意 给定$N$个位置,$2^k$种颜色,让你去涂色,条件是相邻的两种颜色类型异或值的二进制表示不全为$1$(以 ...

  3. 手贱,写个call玩玩.

    今天在睡觉醒时,突然感觉对call和apply有了点理解,但是又不好表达出来.就随便写几个例子. function say() { return this.role; } function Fathe ...

  4. 解析Excel数据

    解析Excel数据常用的方式就是使用POI和JXL工具了,这两种工具使用起来有些类似,这里记录一下使用方式提供个参考 POI使用 File file = new File(filePath); Fil ...

  5. 全国城市空气质量实时数据(PM2.5)实时下载

    国家公布了“http://113.108.142.147:20035/emcpublish/”空气质量实时发布平台,WCF服务地址为“http://113.108.142.147:20035/EnvP ...

  6. 显式锁(四)Lock的等待通知机制Condition

       任意一个Java对象,都拥有一组监视器方法(定义在根类Object上),主要包括:wait( ).wait(long timeout).notify().notifyAll()方法:这些方法与关 ...

  7. 《GPU高性能编程CUDA实战》第九章 原子性

    ▶ 本章介绍了原子操作,给出了基于原子操作的直方图计算的例子. ● 章节代码 #include <stdio.h> #include "cuda_runtime.h" ...

  8. HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 10.Action中的method属性

    转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 在struts1.x中我们知道通过继承DispatchAction可以实现把 ...

  10. Spring boot&Mybatis 启动报错 Failed to auto-configure a DataSource

    *************************** APPLICATION FAILED TO START *************************** Description: Fai ...