Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] main from itcharge:main #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 129 commits into AlgorithmAndLeetCode:main from itcharge:main
May 27, 2022
Merged
Changes from 1 commit
Commits
Show all changes
129 commits
Select commit Hold shift + click to select a range
edf6b32
Create Graph-Bellman-Ford.py
itcharge Mar 20, 2022
afb623a
Update 01.Hash-Table.md
itcharge Mar 21, 2022
5a18692
去除列表中重复题目
itcharge Mar 22, 2022
aeeb26a
更新目录章节列表
Mar 23, 2022
279255b
更新目录章节列表
Mar 23, 2022
a1f3ee0
更新题解列表
Mar 24, 2022
1b4be8a
Update 0001. 两数之和.md
Mar 28, 2022
de513d7
Update 01.Enumeration-Algorithm.md
Mar 28, 2022
19241f7
Update 01.Enumeration-Algorithm.md
Mar 29, 2022
dce7cd0
Update 01.Enumeration-Algorithm.md
Mar 29, 2022
b66ccbd
Update 01.Enumeration-Algorithm.md
Mar 29, 2022
092fa76
更新课程内容链接
Mar 29, 2022
1c77890
更新题解列表
Mar 30, 2022
1ebb2c1
更新课程内容链接
Mar 30, 2022
b6c3567
Update 01.Segment-Tree.md
Apr 1, 2022
5261ecd
Update 0078. 子集.md
Apr 1, 2022
d8949ea
更新题解列表
Apr 1, 2022
d49e419
Create 1450. 在既定时间做作业的学生人数.md
Apr 2, 2022
a12a916
Update LeetCode 解题报告空白.md
Apr 2, 2022
2a8e742
更新题解目录
Apr 2, 2022
8d7d466
Update 0204. 计数质数.md
Apr 6, 2022
c98b70d
Create 1925. 统计平方和三元组的数目.md
Apr 6, 2022
45b74c1
Create 0800. 相似 RGB 颜色.md
Apr 6, 2022
6f01d1c
更新题解列表
Apr 6, 2022
56cec98
Update 02.Algorithm-Complexity.md
Apr 6, 2022
c87996a
Update 0078. 子集.md
Apr 6, 2022
144761a
Update 0090. 子集 II.md
Apr 6, 2022
860f344
Update 剑指 Offer 57 - II. 和为s的连续正数序列.md
Apr 6, 2022
862f2f6
更新章节目录
Apr 7, 2022
c07e2ab
Update 03.LeetCode-Guide.md
Apr 8, 2022
cf216ed
Update 0091. 解码方法.md
Apr 8, 2022
2ce7012
Update 0198. 打家劫舍.md
Apr 8, 2022
c6ab45f
Update 0213. 打家劫舍 II.md
Apr 8, 2022
d616310
Update 1227. 飞机座位分配概率.md
Apr 8, 2022
664351c
Update 1486. 数组异或操作.md
Apr 8, 2022
ee79e53
Update 剑指 Offer II 089. 房屋偷盗.md
Apr 8, 2022
a93edaa
Update 剑指 Offer II 090. 环形房屋偷盗.md
Apr 8, 2022
d2c7af4
Update 0509. 斐波那契数.md
Apr 8, 2022
946591d
Update 0104. 二叉树的最大深度.md
Apr 8, 2022
6f82a91
更新文章代码格式
Apr 8, 2022
7575e44
Update 01.Recursive-Algorithm.md
Apr 8, 2022
31d9afe
Update 01.Recursive-Algorithm.md
Apr 8, 2022
e3c9bf5
Update 01.Recursive-Algorithm.md
Apr 8, 2022
17d0db8
Update 01.Recursive-Algorithm.md
Apr 11, 2022
c132bc5
更改题解标题格式
Apr 12, 2022
5c2dbe6
Update Array-MergeSort.py
Apr 14, 2022
2c373ef
Update 01.Divide-And-Conquer-Algorithm.md
Apr 14, 2022
56e57eb
更新公式格式
Apr 14, 2022
4c788aa
Update 01.Divide-And-Conquer-Algorithm.md
Apr 14, 2022
fbb04e2
Update 01.Recursive-Algorithm.md
itcharge Apr 18, 2022
3b11f52
Update 0303. 区域和检索 - 数组不可变.md
Apr 19, 2022
118b3e1
更新线段树模板
Apr 19, 2022
a0c5060
更新线段树模板
itcharge Apr 19, 2022
a59b2b0
更新题解列表
Apr 20, 2022
01e7dd9
Update 01.Backtracking-Algorithm.md
Apr 20, 2022
f5a441d
更新线段树模板
Apr 21, 2022
d9c77c0
Update 0303. 区域和检索 - 数组不可变.md
Apr 24, 2022
d5c8551
Update 0307. 区域和检索 - 数组可修改.md
Apr 24, 2022
0034d80
Update 0673. 最长递增子序列的个数.md
Apr 24, 2022
934b009
Update 1109. 航班预订统计.md
Apr 24, 2022
1fee32b
Update 1450. 在既定时间做作业的学生人数.md
Apr 24, 2022
ade2d16
Update 0370. 区间加法.md
Apr 24, 2022
a504a23
Create 1310. 子数组异或查询.md
Apr 24, 2022
d58ba3f
Create 0729. 我的日程安排表 I.md
Apr 24, 2022
4cca60b
Create 0731. 我的日程安排表 II.md
Apr 24, 2022
6a19d08
更新题解列表
Apr 24, 2022
4143b23
Update 01.Backtracking-Algorithm.md
Apr 26, 2022
679ac20
Update 01.Backtracking-Algorithm.md
Apr 26, 2022
3722ae9
Update 0046. 全排列.md
Apr 26, 2022
82ed40f
Update 0051. N 皇后.md
Apr 26, 2022
6bfe7db
Update 0078. 子集.md
Apr 26, 2022
7808a33
Update 01.Backtracking-Algorithm.md
Apr 26, 2022
0337985
更新课程信息
Apr 27, 2022
824e3fc
Update 01.Union-Find.md
May 5, 2022
8771e27
Update 01.Graph-Basic.md
May 6, 2022
0b278e3
Update 01.Union-Find.md
May 7, 2022
a55167d
Update 0547. 省份数量.md
May 9, 2022
a02c90f
Update 0990. 等式方程的可满足性.md
May 9, 2022
ffab9eb
更新 并查集文章
May 9, 2022
aa70ec9
更新并查集模板
May 9, 2022
1702217
更新并查集文章
May 9, 2022
76d3ccb
Update 01.Union-Find.md
May 10, 2022
39c00d3
更新贪心算法
May 10, 2022
4906387
Update 0455. 分发饼干.md
May 11, 2022
3fdca82
更新 并查集文章内容
May 11, 2022
ccef0ac
更新 贪心算法文章内容
May 11, 2022
c17114d
Update 0435. 无重叠区间.md
May 11, 2022
bd1586d
Update 0455. 分发饼干.md
May 11, 2022
8ede64f
更新 并查集文章内容
May 11, 2022
50ee50d
Update 01.Greedy-Algorithm.md
May 12, 2022
ed47e94
Create 1710. 卡车上的最大单元数.md
May 12, 2022
2058630
Update 0217. 存在重复元素.md
May 15, 2022
ad0d375
Update 0219. 存在重复元素 II.md
May 15, 2022
1bf455c
Update 0036. 有效的数独.md
May 15, 2022
556b42e
Update Course-Web-03.md
May 16, 2022
1ddf186
更新力扣新域名
May 18, 2022
836a4b2
Create 2276. 统计区间中的整数数目.md
May 18, 2022
7a87eb5
更新题解列表
May 18, 2022
f9ad233
Update 0005. 最长回文子串.md
May 19, 2022
96c49de
Create 1217. 玩筹码.md
May 19, 2022
63546bc
Create 1247. 交换字符使得字符串相同.md
May 19, 2022
f59d852
更新题解列表
May 19, 2022
c072756
Update 1247. 交换字符使得字符串相同.md
May 19, 2022
23a9161
Update 0189. 轮转数组.md
May 20, 2022
99359be
Create 1408. 数组中的字符串匹配.md
May 20, 2022
4a7ac52
Create 2156. 查找给定哈希值的子串.md
May 20, 2022
599bdf3
Update 02.String-Rabin-Karp.md
May 20, 2022
631f9ac
更新题解列表
May 20, 2022
cbd92c5
Update 2156. 查找给定哈希值的子串.md
May 20, 2022
aac56f7
Update 0189. 轮转数组.md
May 23, 2022
1eb829e
Create 1400. 构造 K 个回文字符串.md
itcharge May 23, 2022
676cf1d
Merge branch 'main' of https://github.com/itcharge/LeetCode-Py
itcharge May 23, 2022
7b2a477
Update 1400. 构造 K 个回文字符串.md
May 23, 2022
98f9927
Create 0921. 使括号有效的最少添加.md
May 23, 2022
92e3fa9
Create 1029. 两地调度.md
May 23, 2022
bed1393
Create 1605. 给定行和列的和求可行矩阵.md
May 23, 2022
d7e6d10
Create 0861. 翻转矩阵后的得分.md
May 23, 2022
4858672
更新题解列表
May 23, 2022
5779d8f
Update 0211. 添加与搜索单词 - 数据结构设计.md
May 24, 2022
1091fc3
Create 0846. 一手顺子.md
May 24, 2022
2d93c03
Create 1296. 划分数组为连续数字的集合.md
May 24, 2022
c5147f6
更新题解列表
May 24, 2022
c39e8ea
更新题解列表
May 25, 2022
841f1de
Create 0851. 喧闹和富有.md
May 25, 2022
c5c149e
更新题解列表
May 25, 2022
5723006
Create 0041. 缺失的第一个正数.md
May 26, 2022
e73e9ee
Update 0861. 翻转矩阵后的得分.md
May 26, 2022
b3ce2a4
Create 0201. 数字范围按位与.md
May 26, 2022
c00a40b
更新题解列表
May 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 0051. N 皇后.md
  • Loading branch information
杨世超 committed Apr 26, 2022
commit 82ed40f37c8e50c0548db88e9d07692a4fa462a6
97 changes: 83 additions & 14 deletions Solutions/0051. N 皇后.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,96 @@

## 题目大意

- n 皇后问题:将 n 个皇后放置在 `n * n` 的棋盘上,并且使得皇后彼此之间不能攻击。
- 皇后彼此不能相互攻击:指的是任何两个皇后都不能处于同一条横线、纵线或者斜线上。
**描述**:给定一个整数 `n`。

现在给定一个整数 `n`,返回所有不同的「n 皇后问题」的解决方案。每一种解法包含一个不同的「n 皇后问题」的棋子放置方案,该方案中的 `Q` 和 `.` 分别代表了皇后和空位。
**要求**:返回所有不同的「`n` 皇后问题」的解决方案。每一种解法包含一个不同的「`n` 皇后问题」的棋子放置方案,该方案中的 `Q` 和 `.` 分别代表了皇后和空位。

## 解题思路
**说明**:

经典的回溯问题。使用 chessboard 来表示棋盘,`Q` 代表皇后,`.` 代表空位,初始都为 `.`。然后使用 res 存放最终答案。
- **n 皇后问题**:将 `n` 个皇后放置在 `n * n` 的棋盘上,并且使得皇后彼此之间不能攻击。
- **皇后彼此不能相互攻击**:指的是任何两个皇后都不能处于同一条横线、纵线或者斜线上。
- 1ドル \le n \le 9$。

先定义棋盘合理情况判断方法,判断同一条横线、纵线或者斜线上是否存在两个以上的皇后。
**示例**:

再定义回溯方法,从第一行开始进行遍历。
```Python
输入 n = 4
输出 [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
解释 如下图所示,4 皇后问题存在 2 个不同的解法。
```

- 如果当前行 `row`等于 `n`,则当前棋盘为一个可行方案,将其拼接加入到 res 数组中。
- 遍历 `[0, n]` 列元素,先验证棋盘是否可行,如果可行:
- 将当前行当前列尝试换为 `Q`。
- 然后继续递归下一行。
- 再将当前行回退为 `.`。
- 最终返回 res 数组。
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)

## 解题思路

## 代码
### 思路 1:回溯算法

这道题是经典的回溯问题。我们可以按照行序来放置皇后,也就是先放第一行,再放第二行 ...... 一直放到最后一行。

对于 `n * n` 的棋盘来说,每一行有 `n` 列,也就有 `n` 种放法可供选择。我们可以尝试选择其中一列,查看是否与之前放置的皇后有冲突,如果没有冲突,则继续在下一行放置皇后。依次类推,直到放置完所有皇后,并且都不发生冲突时,就得到了一个合理的解。

并且在放置完之后,通过回溯的方式尝试其他可能的分支。

下面我们根据回溯算法三步走,写出对应的回溯算法。

1. **明确所有选择**:根据棋盘中当前行的所有列位置上是否选择放置皇后,画出决策树,如下图所示。

- ![](https://qcdn.itcharge.cn/images/20220426095225.png)

2. **明确终止条件**:

- 当遍历到决策树的叶子节点时,就终止了。也就是在最后一行放置完皇后时,递归终止。

3. **将决策树和终止条件翻译成代码:**

1. 定义回溯函数:

- 首先我们先使用一个 `n * n` 大小的二维矩阵 `chessboard` 来表示当前棋盘,`chessboard` 中的字符 `Q` 代表皇后,`.` 代表空位,初始都为 `.`。
- 然后定义回溯函数 `backtrack(chessboard, row): ` 函数的传入参数是 `chessboard`(棋盘数组)和 `row`(代表当前正在考虑放置第 `row` 行皇后),全局变量是 `res`(存放所有符合条件结果的集合数组)。
- `backtrack(chessboard, row):` 函数代表的含义是:在放置好第 `row` 行皇后的情况下,递归放置剩下行的皇后。
2. 书写回溯函数主体(给出选择元素、递归搜索、撤销选择部分)。
- 枚举出当前行所有的列。对于每一列位置:
- 约束条件:定义一个判断方法,先判断一下当前位置是否与之前棋盘上放置的皇后发生冲突,如果不发生冲突则继续放置,否则则继续向后遍历判断。
- 选择元素:选择 `row, col` 位置放置皇后,将其棋盘对应位置设置为 `Q`。
- 递归搜索:在该位置放置皇后的情况下,继续递归考虑下一行。
- 撤销选择:将棋盘上 `row, col` 位置设置为 `.`。

```Python
# 判断当前位置 row, col 是否与之前放置的皇后发生冲突
def isValid(self, n: int, row: int, col: int, chessboard: List[List[str]]):
for i in range(row):
if chessboard[i][col] == 'Q':
return False

i, j = row - 1, col - 1
while i >= 0 and j >= 0:
if chessboard[i][j] == 'Q':
return False
i -= 1
j -= 1
i, j = row - 1, col + 1
while i >= 0 and j < n:
if chessboard[i][j] == 'Q':
return False
i -= 1
j += 1

return True
```

```Python
for col in range(n): # 枚举可放置皇后的列
if self.isValid(n, row, col, chessboard): # 如果该位置与之前放置的皇后不发生冲突
chessboard[row][col] = 'Q' # 选择 row, col 位置放置皇后
backtrack(row + 1, chessboard) # 递归放置 row + 1 行之后的皇后
chessboard[row][col] = '.' # 撤销选择 row, col 位置
```

3. 明确递归终止条件(给出递归终止条件,以及递归终止时的处理方法)。
- 当遍历到决策树的叶子节点时,就终止了。也就是在最后一行放置完皇后(即 `row == n`)时,递归停止。
- 递归停止时,将当前符合条件的棋盘转换为答案需要的形式,然后将其存入答案数组 `res` 中即可。

### 思路 1:回溯算法代码

```Python
class Solution:
Expand Down

AltStyle によって変換されたページ (->オリジナル) /