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 的棋盘上,并且使皇后彼此之间 ...
随机推荐
- leetcode个人题解——#22 Generate Parentheses
思路: 递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号. class Solution { public: vector< ...
- MongoDB3.2 集群搭建
一.集群的理论知识 1.1 集群成员 MongoDB的集群类似于GreenPlum集群,由一个入口节点负责任务分发与结果统计,分片结节负责执行任务.不同GP,多了一个config servers. 集 ...
- “hello world!”团队第三次会议
团队“hello world!”团队召开的第三次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 一.会议时间 2017年10 ...
- iOS开发allocWithZone介绍
首先我们知道,我们需要保证单例类只有一个唯一的实例,而平时我们在初始化一个对象的时候, [[Class alloc] init],其实是做了两件事. alloc 给对象分配内存空间,init是对对象的 ...
- <Effective C++>读书摘要--Designs and Declarations<二>
<Item 20> Prefer pass-by-reference-to-const to pass-by-value 1.By default, C++ passes objects ...
- 【APS.NET Core】- Json配置文件的读取
在项目目录下有个 appsettings.json ,我们先来操作这个文件.在appsettings.json中添加以下内容: { "Logging": { "LogLe ...
- Spring Bean注册和加载
Spring解密 - XML解析 与 Bean注册 Spring解密 - 默认标签的解析 Spring解密 - 自定义标签与解析 Spring解密 - Bean的加载流程
- SSM整合步骤
第一步:mybatis和spring整合 mybatis-spring-1.2.2:是mybatis官方出的包: mybatis的包: mybatis和spring的整合包: spring及sprin ...
- RT-thread 设备驱动组件之PIN设备
在RT-thread 2.0.0正式版中引入了pin设备作为杂类设备,其设备驱动文件pin.c在rt-thread-2.0.1\components\drivers\misc中,主要用于操作芯片GPI ...
- RT-thread内核之邮箱
一.邮箱控制块:在include/rtdef.h中 #ifdef RT_USING_MAILBOX /** * mailbox structure */ struct rt_mailbox { str ...