题目:

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)
{
int ret = ;
if ( n== ) return ret;
vector<bool> colUsed(n,false), diagUsed1(*n-,false), diagUsed2(*n-,false);
Solution::dfs(ret, , n, colUsed, diagUsed1, diagUsed2);
return ret;
}
static void dfs( int &ret, int row, int n, vector<bool>& colUsed, vector<bool>& diagUsed1, vector<bool>& diagUsed2 )
{
if ( row==n ) { ret++; return; }
for ( size_t col = ; col<n; ++col ){
if ( !colUsed[col] && !diagUsed1[col+n--row] && !diagUsed2[col+row] )
{
colUsed[col] = diagUsed1[col+n--row] = diagUsed2[col+row] = true;
Solution::dfs(ret, row+, n, colUsed, diagUsed1, diagUsed2);
diagUsed2[col+row] = diagUsed1[col+n--row] = colUsed[col] = false;
}
}
}
};

tips:

如果还是用深搜的思路,这个N-Queens II要比N-Queens简单一些,可能是存在其他的高效解法。

===========================================

第二次过这道题,经过了N-Queens,这道题顺着dfs的思路就写下来了。

class Solution {
public:
int totalNQueens(int n)
{
int ret = ;
if ( n< ) return ret;
vector<bool> colUsed(n, false);
vector<bool> r2l(*n-, false);
vector<bool> l2r(*n-, false);
Solution::dfs(ret, n, , colUsed, r2l, l2r);
return ret;
}
static void dfs(
int& ret,
int n,
int index,
vector<bool>& colUsed,
vector<bool>& r2l,
vector<bool>& l2r)
{
if ( index==n )
{
ret++;
return;
}
for ( int i=; i<n; ++i )
{
if ( colUsed[i] || r2l[i-index+n-] || l2r[i+index] ) continue;
colUsed[i] = true;
r2l[i-index+n-] = true;
l2r[i+index] = true;
Solution::dfs(ret, n, index+, colUsed, r2l, l2r);
colUsed[i] = false;
r2l[i-index+n-] = false;
l2r[i+index] = false;
}
}
};

【N-Quens II】cpp的更多相关文章

  1. 【Word Break II】cpp

    题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...

  2. 【Unique Paths II】cpp

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  3. 【Path Sum II】cpp

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  4. 【Spiral Matrix II】cpp

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  5. 【palindrome partitioning II】cpp

    题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...

  6. 【Jump Game II 】cpp

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  7. 【Combination Sum II 】cpp

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  8. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  9. 【Single Num II】cpp

    题目: Given an array of integers, every element appears three times except for one. Find that single o ...

随机推荐

  1. 云为 | 提供海外 IT 人才派遣、猎头、人力资源外包服务

    云为是大连信为软件开发有限公司为人力资源外包服务创建的品牌,是中国专业的人力资源外包领域的服务商,在信息技术行业为海外企业雇主招聘合格.专业且技能熟练的精英人士.我们的客户涵盖了日本上市公司和​​株式 ...

  2. EBS应用重启

    重启系统应用 cd $ADMIN_SCRIPTS_HOME ./adstpall.sh apps/apps ./adstrtal.sh apps/apps 在重启应用时,可能会出现并发管理器未启动的情 ...

  3. thymeleaf 模板语言简介

    参考网址: https://blog.csdn.net/mlin_123/article/details/51816533 1.1 Thymeleaf 在有网络和无网络的环境下皆可运行,而且完全不需启 ...

  4. ansible 2.1.0 api 编程

    pdf文档 https://media.readthedocs.org/pdf/ansible/latest/ansible.pdf api介绍 http://blog.csdn.net/python ...

  5. Intel&amd

  6. 配置dubbo架构的maven项目

    1. 拆分工程 1)将表现层工程独立出来: e3-manager-web 2)将原来的e3-manager改为如下结构 e3-manager |--e3-manager-dao |--e3-manag ...

  7. Echarts样式

    Echarts设置样式如下 <div id="main" style="width: 250px;height:200px;"></div&g ...

  8. 问题005:如何配置JDK,Java运行环境?

    方法一:我的电脑右击-->属性-->高级-->环境变量-->Path 方法二:set path是查询环境变灵, set path=路径

  9. 数据结构(C语言)分享笔记:数据结构的逻辑层次、存储层次

    [1] 严格意义上数据结构的概念 数据结构,一个简单的定义:相互之间存在一种或多种特定关系的数据元素的集合.即:数据结构 = 元素集合 + 元素间关系的集合 . 在讨论数据结构时,可以基于两个不同的层 ...

  10. SAP系统管理中常见问题解答(转载)

    1.如何查看SAP系统的位数? system——status看 Platform ID Platform 32-bit 64-bit --------------------------------- ...