题目:

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. Thymeleaf的模板使用介绍

    参考网址: https://blog.csdn.net/hry2015/article/details/73476973 先定义一个html文件, 如下: 文件路径: templates/templa ...

  2. Notification高级技巧

    观察Notification这个类,你会发现里面还有很多我们没有使用过的属性.先来看看sound这个属性吧,它可以在通知发出的时候播放一段音频,这样就能够更好地告知用户有通知到来.sound 这个属性 ...

  3. 【extjs6学习笔记】1.15 初始: 关于build

    调试版本 sencha app build --development 发布版本 sencha app build 说明: 使用第三方库时,目前sencha可能还有bug,会更改第三方库内容,所以发布 ...

  4. draggable与overflow同时存在,无法拖拽出父元素问题解决

    在使用jquery-ui的拖拽功能对列表内的选项拖拽时,发现无法将选项拖拽出列表的范围,一出范围就自动隐藏在列表下,查找到最后的原因是css中的overflow的原因,overflow存在则不能将选项 ...

  5. windows下php7.1.5、mysql环境搭建

    php http://windows.php.net/download/ 如果是使用ISAPI的方式来运行PHP就必须用Thread Safe(线程安全)的版本:而用FastCGI模式运行PHP的话就 ...

  6. DBMS事务的四大特性

    数据库事务的四大特性分别是:原子性.一致性.隔离性和持久性. 特性 说明 实现 一致性 在一个事务执行之前和执行之后数据库都必须处于一致性状态.假如数据库的状态满足所有的完整性约束,也可以说数据库是一 ...

  7. 使用JDBC操作SAP云平台上的HANA数据库

    本文假设您对JDBC(Java Database Connectivity)有最基本的了解.您也可以将其同ADBC(ABAP Database Connectivity)做对比,细节请参考我的博客AD ...

  8. selenium 使用键盘时 提示java.lang.IllegalArgumentException: Key Down / Up events only make sense for modifier keys.

    输入某个内容后,使用enter键进行确认,最开始使用方式为: driver.findElement(By.xpath("//input[@name='supplier_name'][@id= ...

  9. 漫谈 Clustering (番外篇): Expectation Maximization

    Expectation Maximization (EM) 是一种以迭代的方式来解决一类特殊最大似然 (Maximum Likelihood) 问题的方法,这类问题通常是无法直接求得最优解,但是如果引 ...

  10. C#继承机制 C#中的继承符合下列规则

    1.继承是可传递的.如果C从B中派生,B又从A中派生,那么C不仅继承了B中声明的成员,同样也继承了A中的成员.Object 类作为所有类的基类. 2.派生类应当是对基类的扩展.派生类可以添加新的成员, ...