题目:

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. Google常用拓展插件

    1.web前端助手(FEhelper)提供一些实用的前端小工具,功能十分贴心 2.bookMarks Manager 一个书签管理工具 3.Clear Cache 清除浏览器的缓存,有很多供选择的条目 ...

  2. iOS核心动画高级技巧之CALayer(一)

    iOS核心动画高级技巧之CALayer(一) iOS核心动画高级技巧之图层变换和专用图层(二)iOS核心动画高级技巧之核心动画(三)iOS核心动画高级技巧之性能(四)iOS核心动画高级技巧之动画总结( ...

  3. notepad++ 等用正则表达式自动添加sql引号(宏)

    一般sql语句会经常用到给括号里的内容添加引号,sql如下 Select * From Test ', ', ', ', ', '); 一开始参考了http://blog.sina.com.cn/s/ ...

  4. Riverbed SteelHead 9.5.0

    平台: scientific linux release 6.5 类型: 虚拟机镜像 软件包: riverbed steelhead 9.5.0 basic software Enterprise i ...

  5. Yii2 Working with Relational Data at ActiveDataProvider

    Yii2 Working with Relational Data at ActiveDataProvider namespace common\models; use Yii; use yii\ba ...

  6. pat乙级1049

    浮点型乘整型和整型乘浮点型结果不同,不知为什么. double sum = 0.0; ; i < n; i++) { cin >> a[i]; sum += a[i] * (i + ...

  7. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

  8. 循环 -----JavaScript

    本文摘要:http://www.liaoxuefeng.com/ JavaScript的循环有两种,一种是for循环,通过初始条件.结束条件和递增条件来循环执行语句块: var x = 0; var ...

  9. 魅族MX3 Flyme3.0找回手机功能支持远程拍照密码错两次自动拍照

    进入Flyme页面(http://app.meizu.com/),选择“查找手机”即可进行查找自己登记的魅族系列手机. 如果您在一个账号下登记过N多魅族系列手机,那么都是可以进行查找的,参见下图 魅族 ...

  10. C#继承机制 继承与访问修饰符

    继承与访问修饰符 访问修饰符是一些关键字,用于指定声明的成员或类型的可访问性.类的继承中有四个访问修饰符: public protected internal private.使用这些访问修饰符可指定 ...