【N-Quens II】cpp
题目:
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的更多相关文章
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【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 ...
- 【Spiral Matrix II】cpp
题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...
- 【palindrome partitioning II】cpp
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- 【Jump Game II 】cpp
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 【Combination Sum II 】cpp
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 【Single Num II】cpp
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
随机推荐
- Java正则表达式—小应用—简易爬虫
在上一篇中,学习了正则表达式的四个功能.即匹配.分割.替换.获取. 利用获取功能,可以实现简单的网页爬虫. 4,获取:将字符串中的符合规则的子串取出. 获取功能的操作步骤: 1,将正则表达式 ...
- Oracle SQL Developer-3.2.20.09.87 Windows 10启动问题处理&配置
用了好多年的工具,准备在笔记本上使用时启动不了,但在办公室PC上可以正常使用.两者电脑OS都一样,一个是全新安装.一个是从Windows 7升级而来.下载了最新版发现版本到17了,Oracle刷版本号 ...
- fish 与oh-my-fish 的安装
fish 相对于 自带的shell优势很大,最近在研究使用中. 安装教程如下: sudo apt-get install fish oh-my-fish是github上开源项目,使得fish的使用更加 ...
- 服网LNMP集群-1.0.5
平台: arm 类型: ARM 模板 软件包: haproxy linux mysql nginx application server arm basic software fuwang infra ...
- windows 7 X64 提示“com surrogate 已停止工作”的解决方案
C:\Windows\SysWOW64\dllhost.exe 把以上文件添加至“数据执行保护”.
- linux 命令——14 head (转)
head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...
- http请求头和相应头的作用
请求头(Request Headers) Accept:application/json, text/plain, */* Accept-Encoding:gzip, deflate Accept-L ...
- IOS 控件器的创建方式(ViewController)
● 控制器常见的创建方式有以下几种 ➢ 通过storyboard创建 ➢ 直接创建 NJViewController *nj = [[NJViewController alloc] init]; ➢ ...
- SpringBoot学习2:springboot整合servlet
整合方式1:通过注解扫描完成 Servlet 组件的注册 1.编写servlet package com.bjsxt.servlet; import javax.servlet.ServletExce ...
- css与html结合四种方式
方式一:每个标签加一个属性法 <div style="background-color:red;color:green;"></div> 方式二:head中 ...