【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 ...
随机推荐
- expr判断文件名以固定格式结尾
#!/bin/bash if expr "$1" : ".*\.sh" &>/dev/null then echo "okok" ...
- Linux关机/重启/用户切换/注销
目录 1. 关机/重启命令 2. 用户切换/注销 2.1 基本说明 2.2 切换用户 2.3 注销用户 1. 关机/重启命令 # shutdown命令 shutdown -h now # 立即关机 s ...
- mysql—从字符串中提取数字(类型1)
select reason,CHAR_LENGTH(reason),mid(reason,5,CHAR_LENGTH(reason)-5)+0 from `table` 解释: CHAR_LENGTH ...
- 系列好文 | Kubernetes 弃用 Docker,我们该何去何从?
作者 | 张攀(豫哲) 来源 | 尔达 Erda 公众号 导读:Erda 作为一站式云原生 PaaS 平台,现已面向广大开发者完成 70w+ 核心代码全部开源!**在 Erda 开源的同时,我们计划编 ...
- AI作曲的一个点子
通常的AI作曲都是通过拆分音乐为几个声道, 然后再把各个声道拆成音符去分析. 我忽然之间有个想法,是否可以继续拆分下去. 音符就是一些有规则的高低电平,这样把音符拆成电平. 一定会带来巨大的运算,但如 ...
- Android 开源框架Universal-Image-Loader加载https图片
解决方案就是 需要 android https HttpsURLConnection 这个类忽略证书 1,找到 Universal-Image-Loader的library依赖包下面com.nostr ...
- 队列——Java实现
1 package struct; 2 3 interface IQueue{ 4 //入队列 5 void add(Object obj); 6 //出队列 7 Object remove(); 8 ...
- Android 图片框架
1.图片框架:Picasso.Glide.Fresco 2.介绍: picasso:和Square的网络库能发挥最大作用,因为Picasso可以选择将网络请求的缓存部分交给了okhttp实现 Glid ...
- 查看linux系统CPU和内存命令
cat /proc/cpuinfo查看linux系统的CPU型号.类型以及大小,如下图所示. 通过greap命令根据Physical Processor ID筛选出多核CPU的信息. cat ...
- LR中的快捷建
Ctrl+F 弹出搜索对话框 CTRL+F8 弹出view tree 界面 (寻找关联) 觉得不错的可关注微信公众号在手机上观看,让你用手机边玩边看