【LeetCode】N皇后I
【问题】n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。
每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。
示例: 输入:
输出:
[[".Q..", // 解法 1
"…Q",
"Q…",
"..Q."],
["..Q.", // 解法 2
"Q…",
"…Q",
".Q.."]
]
解释: 皇后问题存在两个不同的解法。
【思路】N皇后在不同地方,不同场合都有听到过这个问题,但仔细分析了一下,发现和原来的数独问题十分的类似,也是约束编程+回溯法的思想!我们首先分析一下两者的相同点和不同点:
解数独问题:
N确定,为9x9的网格,约束条件为:向未知位置填入1-9的数字,使得该数所在的行和列均不重复以及所在的3x3网格内也不重复,因此我们需要使用col_[9][9]、row_[9][9]、block_[9][9]来储存数字在行、列和网格中是否被使用过。使用过标记为true,否则标记为false。
N皇后问题:
N不确定,因此我们需要在函数中建立辅助空间,而不能建立成成员变量,约束条件为:在NxN的网格中任意摆放皇后Q,为了避免皇后之间不能相互攻击,该位置所在的行、列以及主、副对角线均只能有这一个Q,因此我们需要使用cols_标记每一列是否存在Q,使用diag1s_、diag2s_来标记主、副对角线是否存在Q。
当了解到这些之后,整个回溯递归方法就十分清晰了,中间有一个地方十分让人困惑,ll和rr是如何求解的呢?这就要说到主、副对角线的性质了!!!
主对角线:col+row的值是一定的,范围[0, 2n-2],从零开始
副对角线:col-row的值是一定的,为了使索引有效,加上定值n-1, 最终范围[0, 2n-2]
有人会问,row怎么遍历,emmm, 递归修改的参数就是row,其实也就相当于遍历了整个网格,但速度还可以,中间存在了很多条件判断进行剪枝!
class Solution {
public:
void solve(vector<vector<string>>& res, vector<string>& tmp, vector<bool>& cols_, vector<bool>& diag1s_, vector<bool>& diag2s_, int n, int row){
if(row == n){
res.push_back(tmp);
return;
}
for(auto col = ; col < n; col++){
int ll = row + col;
int rr = row - col + n - ;
if (cols_[col] && diag1s_[ll] && diag2s_[rr]){
tmp[row][col] = 'Q';
cols_[col] = false;
diag1s_[ll] = false;
diag2s_[rr] = false;
solve(res, tmp, cols_, diag1s_, diag2s_, n, row+);
tmp[row][col] = '.';
cols_[col] = true;
diag1s_[ll] = true;
diag2s_[rr] = true;
}
}
}
vector<vector<string>> solveNQueens(int n) {
vector<vector<string>> res;
vector<string> tmp(n, string(n, '.'));
vector<bool> cols_(n, true);
vector<bool> diag1s_(*n-, true);
vector<bool> diag2s_(*n-, true);
solve(res, tmp, cols_, diag1s_, diag2s_, n, );
return res;
}
};
【LeetCode】N皇后I的更多相关文章
- [LeetCode] N皇后问题
LeetCode上面关于N皇后有两道题目:51 N-Queens:https://leetcode.com/problems/n-queens/description/ 52 N-Queens II: ...
- LeetCode N皇后 & N皇后 II
题目链接:https://leetcode-cn.com/problems/n-queens/ 题目链接:https://leetcode-cn.com/problems/n-queens-ii/ 题 ...
- [LeetCode] N-Queens II N皇后问题之二
Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...
- [LeetCode] N-Queens 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皇后 II(N-Queens II)
Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...
- Leetcode之回溯法专题-51. N皇后(N-Queens)
Leetcode之回溯法专题-51. N皇后(N-Queens) n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给 ...
- [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] 51. N-Queens 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-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 ...
- LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...
随机推荐
- CSP-S 2019 复赛游记
自闭游记 >_< Day 0 随便敲了一些板子 当然打了摆. 奶人的话写满了俩黑板啊,没人奶我可海星. 晚上没怎么打摆,随便敲了几道板子,然后很早就回去睡了. Day 1 平静地出发了.. ...
- [转载]JDK自带的实用工具——native2ascii.exe
做Java开发的时候,常常会出现一些乱码,或者无法正确识别或读取的文件,原因是编码方式的不一致.native2ascii是sun java sdk提供的一个工具.用来将别的文本类文件(比如*.txt, ...
- pyhton机器学习入门基础(机器学习与决策树)
//2019.07.26#scikit-learn数据挖掘工具包1.Scikit learn是基于python的数据挖掘和机器学习的工具包,方便实现数据的数据分析与高级操作,是数据分析里面非常重要的工 ...
- zabbix监控memcached服务
zabbix监控memcached服务 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装并配置memcached服务 1>.使用yum方式安装memcached [ro ...
- HiBench成长笔记——(7) 阅读《The HiBench Benchmark Suite: Characterization of the MapReduce-Based Data Analysis》
<The HiBench Benchmark Suite: Characterization of the MapReduce-Based Data Analysis>内容精选 We th ...
- mongodb - 关联字段
1,博客表结构 Blog.js var mongoose = require('mongoose') mongoose.connect('mongodb://localhost/test',{ us ...
- css文字实例锦集
在画布上创建向上的3D拉影文字 <canvas id="myCanvas" width="410" height="130">& ...
- leetcode1019 Next Greater Node In Linked List
""" We are given a linked list with head as the first node. Let's number the nodes in ...
- openssl生成CA签署 及 加密解密基础
openssl 生成私有CA 及签署证书 openssl 配置文件: /etc/pki/tls/openssl.cnf 1. 在openssl CA 服务器端生成私钥 cd /etc/pki/CA/ ...
- Docker-harbor-V1.3.0 ”私有仓库“搭建 Easy
准备: centos 7.0 Docker version 1.12.6 docker-compose version 1.19.0 1: updata-yum: 更新yum 源 ...