Leetcode:52. N-QueensII
Description
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.

思路
- 递归方式
- 主要是要判断当前位置是否是合理位置,参考isValid函数
代码
class Solution {
public:
int totalNQueens(int n) {
int res = 0;
if(n == 0) return 0;
vector<string> flag(n, string(n, '.'));
getResult(flag, n, 0, res);
return res;
}
void getResult(vector<string> &flag, int n, int rows, int &res){
if(rows == n){
res++;
return;
}
else{
for(int i = 0; i < n; ++i){
if(isValid(flag, rows, i, n)){
flag[rows][i] = 'Q';
getResult(flag, n, rows + 1, res);
flag[rows][i] = '.';
}
}
}
}
bool isValid(vector<string> &nums, int rows, int cols, int n){
for(int i = 0; i < rows; ++i)
if(nums[i][cols] == 'Q') return false;
for(int i = rows - 1, j = cols - 1; i >= 0 && j >= 0; --i, --j)
if(nums[i][j] == 'Q') return false;
for(int i = rows - 1, j = cols + 1; i >= 0 && j < n; --i, ++j)
if(nums[i][j] == 'Q') return false;
return true;
}
};
Leetcode:52. N-QueensII的更多相关文章
- [LeetCode] 52. N-Queens II N皇后问题之二
The n-queens puzzle is the problem of placing nqueens on an n×n chessboard such that no two queens a ...
- LeetCode - 52. N-Queens II
52. N-Queens II Problem's Link --------------------------------------------------------------------- ...
- Leetcode 52 N-Queens II 回溯搜索
对于N-Queens的每种情况,回答出每种情况的N-Queens的排列数. l,r和c是每种类型的格子是否有棋子. l判断的是这样的对角线的格子 r判断的是这样的对 ...
- [LeetCode] 52. N-Queens II N皇后问题 II
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- Java实现 LeetCode 52 N皇后 II
52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...
- LeetCode(52)-Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 --> ...
- [leetcode]52. N-Queens II N皇后
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- Leetcode 52
//N皇后的基础上改了一点class Solution { public: int totalNQueens(int n) { ; vector<); DFS(pos,,res); return ...
- [LeetCode] 52. N皇后 II
题目链接 : https://leetcode-cn.com/problems/n-queens-ii/ 题目描述: n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间 ...
随机推荐
- geth账户密码
xiaocong geth账户密码 123 {d6abe909013d8da914ae2a08c9b58e7b76601b39} 账户密码 123456 0x4A7F15104F54dB3214D2F ...
- Python3.5在Windows7环境下Scrapy库的安装
Python3.5在Windows7环境下Scrapy库的安装 忙活了一下午,总算是把Scrapy库给装完了,记下来给需要帮助的人 首先安装的环境:Windows7 64位 Python的版本是:3. ...
- Java常用类之String
String 类: 1. java.lang.String 类代表不可变的字符序列: 2. “XXX” 为该类的一个对象: 3. String 类的常用构造方法: ① String(String o ...
- angular4中使用jquer插件
有以下办法 1 在html文档头部引入jquery插件依赖,但是文档一旦变动就麻烦了 2 使用指令:http://www.cnblogs.com/liuyt/p/5810100.html 指令是把利器 ...
- 织梦dede:list标签在列表页同一文章显示两次的解决方法
在列表页用{dede:list}标签调用文章的时候出现了同一篇文章显示两次的问题,经过一天的奋战最后终于解决了,下面CMS集中营站长简单说下我的解决过程来供各位学友参考:1.怀疑是不是每次添加都会自动 ...
- BZOJ 1221 软件开发(费用流)
容易看出这是显然的费用流模型. 把每天需要的餐巾数作为限制.需要将天数拆点,x’表示每天需要的餐巾,x’’表示每天用完的餐巾.所以加边 (s,x',INF,0),(x'',t,INF,0). 餐巾可以 ...
- BZOJ4850/BZOJ2216 JSOI2016灯塔/Poi2011Lightning Conductor(决策单调性)
即对每个i最大化hj-hi+sqrt(|i-j|).先把绝对值去掉,正反各做一次即可.注意到当x>y时,sqrt(x+1)-sqrt(x)<sqrt(y+1)-sqrt(y),所以若对于i ...
- Python fileinput模块详解
Python的fileinput模块可以快速对一个或多个文件进行循环遍历. import fileinput for line in fileinput.input(): process(line) ...
- Android SDK Manager下载,解决方案
一.Windows 平台 在C:\Windows\System32\drivers\etc\hosts文件.添加一行:74.125.237.1 dl-ssl.google.com 二.Li ...
- 【以前的空间】BIT的两个小小运用
剩下一点点时间,就来说说最近才会的关于bit的两个妙用. 求一组数的逆序对 求最长不下降序列 其实两个东西思想差不多,就已第一个为例讲讲. 就是所有数排一遍后,再按照原序列顺序(从后往前),做如下操作 ...