【问题】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的更多相关文章

  1. [LeetCode] N皇后问题

    LeetCode上面关于N皇后有两道题目:51 N-Queens:https://leetcode.com/problems/n-queens/description/ 52 N-Queens II: ...

  2. LeetCode N皇后 & N皇后 II

    题目链接:https://leetcode-cn.com/problems/n-queens/ 题目链接:https://leetcode-cn.com/problems/n-queens-ii/ 题 ...

  3. [LeetCode] N-Queens II N皇后问题之二

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  4. [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 ...

  5. Leetcode之回溯法专题-52. N皇后 II(N-Queens II)

    Leetcode之回溯法专题-52. N皇后 II(N-Queens II) 与51题的代码80%一样,只不过52要求解的数量,51求具体解,点击进入51 class Solution { int a ...

  6. Leetcode之回溯法专题-51. N皇后(N-Queens)

    Leetcode之回溯法专题-51. N皇后(N-Queens) n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

  10. LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天我们讲的是LeetCode的31题,这是一道非常经典的问题,经常会在面试当中遇到.在今天的文章当中除了关于题目的分析和解答之外,我们还会 ...

随机推荐

  1. osgb的顶点,纹理,索引,UV读取与存储

    virtual void apply(osg::Geode& node) { ; i < node.getNumDrawables(); i++) { osg::Geometry* ge ...

  2. ch8 faux列

    在一个布局中,假设有导航元素和内容元素,切给他们都分别应用了背景,理想情况下,背景应该拉长到整个布局的最大高度,从而形成列的效果,但是实际上,因为导航元素没有扩展到最大高度,所以它们的背景不会拉长,如 ...

  3. IDEA代码检验出错

    该软件功能过于强大,会自动检验执行所需要的代码,所以会报错,但实际上我们有写 例如 解决方法 File-->setting 将error改为warning

  4. js获取cookie提取用户名asp.net+html

    JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的. 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一 ...

  5. 动态代理,AOP和Spring

    笔记 1. 什么是AOP? Aspect-Oriented Programming 面向切面编程,关注一个统一的切面,相对于OOP(面向对象编程). 适合的场景: 日志 缓存 鉴权 如果用OOP来做怎 ...

  6. 【HITB GSEC CTF 2017】1000levels

    https://files.cnblogs.com/files/p4nda/498a3f10-8976-4733-8bdb-30d6f9d9fdad.gz #通过阅读天枢战队大佬们的wp调试的结果 首 ...

  7. Oracle修改密码

    1. 登陆oracle sqlplus '/as sysdba' 2. 修改密码 ALTER USER 用户名IDENTIFIED BY 要修改的密码 ; 3.解锁 alter user 用户名 ac ...

  8. hello程序的运行过程-从计算机系统角度

    hello程序的运行过程-从计算机系统角度 1.gcc编译器驱动程序读取源程序文件hello.c,并将它翻译成一个可执行目标文件hello.翻译过程分为四个阶段:预处理阶段,编译阶段,汇编阶段,链接阶 ...

  9. WEB前端资源项目整合

    WEB前端资源项目整合 vue.js高仿饿了么(1-13章全)链接:https://pan.baidu.com/s/1qYSiYXluA1AlEV0EskxWZw提取码:25z9 Vue.js 2.5 ...

  10. 使用Indy解决Could not load SSL Library错误

    测试平台:DelphiXE7 + Indy 10.6.0.5169 + Win7 64bit 步骤: 1. SSL下载版本:openssl-1.0.1j-i386-win32 可去http://yun ...