【LeetCode】51. N-Queens 解题报告(C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/n-queens/
题目描述
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens’ placement, where 'Q'
and '.'
both indicate a queen and an empty space respectively.
Example:
Input: 4
Output: [
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above.
题目大意
求n皇后问题的每个解。注意题意,n皇后问题是在n*n的棋盘上放n个皇后,每一种放置方案。
解题方法
回溯法
这个题是52. N-Queens II题目的拓展,52题只要求了统计个数,这个题要求把每一种结果写出来。其实代码基本一样了,只是最后在搜索到对应的解的时候,52题只把结果+1即可,现在的这个题需要构造出对应的放置方案,然后放入到res中。
所以定义了两个函数:helper函数是我们即将要处理row行了,进行回溯;isValid是我们如果这一步放在第row,col位置,合不合法?
python代码如下:
class Solution {
public:
vector<vector<string>> solveNQueens(int n) {
vector<int> board(n, -1);
vector<vector<string>> res;
helper(board, res, 0);
return res;
}
// how to put in row? (havent put down yet)
void helper(vector<int>& board, vector<vector<string>>& res, int row) {
const int N = board.size();
if (row == N) {
vector<string> path(N, string(N, '.'));
for (int i = 0; i < N; i++) {
path[i][board[i]] = 'Q';
}
res.push_back(path);
} else {
for (int col = 0; col < N; col++) {
board[row] = col;
if (isValid(board, row, col)) {
helper(board, res, row + 1);
}
board[row] = -1;
}
}
}
// have put down in (row, col), alright?
bool isValid(vector<int>& board, int row, int col) {
for (int prow = 0; prow < row; prow ++) {
int pcol = board[prow];
if (pcol == col || abs(prow - row) == abs(pcol - col)) {
return false;
}
}
return true;
}
};
日期
2018 年 12 月 23 日 —— 周赛成绩新高
【LeetCode】51. N-Queens 解题报告(C++)的更多相关文章
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
随机推荐
- Xpath解析库的使用
### Xpath常用规则 ## nodename 选取此节点的所有子节点 ## / 从当前节点选取直接子节点 ## // 从当前节点选取子孙节点 ## . 选取当前节点 ## .. 选取当前节点的父 ...
- 搭建zabbix服务器常见问题解析处理
1. 找不到url 2. 服务器无法处理当前请求,PHP解析出错 3. 服务器无法处理当前请求,权限不足 1. 找不到url 浏览器报错:The requested URL /zabbix/ was ...
- 简单mvc框架核心笔记
简单mvc框架核心笔记 看了thinkphp5的源码,模仿写了一个简单的框架,有一些心得笔记,记录一下 1.目录结构 比较简单,没有tp那么复杂,只是把需要的核心类写了一些. 核心类库放在mykj里, ...
- C/C++ Qt 数据库与TreeView组件绑定
在上一篇博文<C/C++ Qt 数据库QSql增删改查组件应用>介绍了Qt中如何使用SQL操作函数,并实现了对数据库的增删改查等基本功能,从本篇开始将实现数据库与View组件的绑定,通过数 ...
- 日常Java 2021/11/6
Java多线程编程 Java给多线程编程提供了内置的支持.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个钱程,每条线程并行执行不同的任务.多线程是多任务的一种特别的形式,但多线程使用 ...
- 学习java 7.13
学习内容: 一个汉字存储:如果是GBK编码,占用2个字节:如果是UTF-8编码,占用3个字节 汉字在存储的时候,无论选择哪种编码存储,第一个字节都是负数 字符流=字节流+编码表 采用何种规则编码,就要 ...
- nodejs-os模块
JavaScript 标准参考教程(alpha) 草稿二:Node.js os模块 GitHub TOP os模块 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 ...
- android studio 使用 aidl(三)权限验证
这篇文章是基于android studio 使用 aidl (一) 和 android studio 使用 aidl(二) 异步回调 下面的代码都是简化的,如果看不懂请先移步上2篇文章 网上的东西太坑 ...
- DOM解析xml学习笔记
一.dom解析xml的优缺点 由于DOM的解析方式是将整个xml文件加载到内存中,转化为DOM树,因此程序可以访问DOM树的任何数据. 优点:灵活性强,速度快. 缺点:如果xml文件比较大比较复杂会占 ...
- Dubbo消费者异步调用Future使用
Dubbo的四大组件工作原理图,其中消费者调用提供者采用的是同步调用方式.消费者对于提供者的调用,也可以采用异步方式进行调用.异步调用一般应用于提供者提供的是耗时性IO服务 一.Future异步执行原 ...