leetcode 52 N皇后问题 II
51的简化版,省去根据排列话棋盘的工作,直接计数,代码:
class Solution {
public:
int totalNQueens(int n) {
int res=;
vector<int> pos(n,-);
dfs(n,,pos,res);
return res;
}
void dfs(int n,int row,vector<int>& pos,int &res){
if(row==n){
res++;return;
}
for(int col=;col<n;col++){
if(isValid(row,col,pos)){
pos[row]=col;
dfs(n,row+,pos,res);
pos[row]=-;
}
}
}
bool isValid(int row,int col,vector<int>&pos){
for(int i=;i<row;i++){
if(col==pos[i] || abs(col-pos[i])==abs(row-i))
return false;
}
return true;
}
};
leetcode 52 N皇后问题 II的更多相关文章
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- [LeetCode] 52. N皇后 II
题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...
- Leetcode之回溯法专题-52. N皇后 II(N-Queens II)
Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...
- leetcode 51. N皇后 及 52.N皇后 II
51. N皇后 问题描述 n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回所有不同的 n 皇后 ...
- lintcode 中等题:N Queens II N皇后问题 II
题目: N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 解题: 和上一题差不多,这里只是求数量,这个题目定义全局变量,递 ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- lintcode-34-N皇后问题 II
34-N皇后问题 II 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. 样例 比如n=4,存在2种解决方案 标签 递归 思路 参考http://www.cnblogs.com ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- Leetcode:面试题68 - II. 二叉树的最近公共祖先
Leetcode:面试题68 - II. 二叉树的最近公共祖先 Leetcode:面试题68 - II. 二叉树的最近公共祖先 Talk is cheap . Show me the code . / ...
随机推荐
- Aliyun-Centos 7 LNMP安装(最新版LNMP)
linux装软件方式:1.源码安装:下载wget-->解压tar -zxvf -->配置 ./configure --->编译make -->安装 make install 2 ...
- 增强 Jupyter Notebook的功能
增强 Jupyter Notebook的功能 Jupyter Notebook 是所有开发者共享工作的神器,它为共享 Notebooks 提供了一种便捷方式:结合文本.代码和图更快捷地将信息传达给受众 ...
- ISC2016训练赛 phrackCTF--Smali
测试文件:https://static2.ichunqiu.com/icq/resources/fileupload/phrackCTF/REVERSE/Crackme.smali 参考资料:http ...
- html的图片移动(js)
<!DOCTYPE html><html><style> *{padding: 0;margin: 0} #open{ width: 300px; height: ...
- 基于Nginx+nginx-rtmp-module+ffmpeg搭建rtmp、hls流媒体服务器(二)
前言 Nginx-rtmp-module插件针对RTMP协议中一些命令,实现了事件通知和exec外部脚本处理.这里我通过一个简单的SpringBoot项目和Python代码,快速搭建一个HTTP服务来 ...
- Java List对象集合按对象属性分组、分组汇总、过滤等操作示例
import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Col ...
- iOS下设备版本获取
执行环境 可以从 UIDevice 的属性 model 得到在现在执行的环境.例子如下: 1 2 3 4 5 6 7 8 9 10 NSString *modelname = [[UIDevice c ...
- centos7 安装 tesseract4.1
官网大法好,其他方法需要装好多依赖,还没安装成功... yum-config-manager --add-repo https://download.opensuse.org/reposito ...
- Educational Codeforces Round 68 (Rated for Div. 2) C. From S To T (字符串处理)
C. From S To T time limit per test1 second memory limit per test256 megabytes inputstandard input ou ...
- 第一次接触oracle
登录 SQLPLUS cmd sqlplus [用户名]/[密码][@数据库] [参数] sqlplus sys/orcl as sysdba -- 登录 sys 用户,必须指定 sysdba 或 s ...