Update n_queens_problem.md

This commit is contained in:
krahets 2024-07-30 16:34:31 +08:00
parent 0f865d2727
commit 8655c7b3ca
2 changed files with 7 additions and 3 deletions

View File

@ -8,7 +8,7 @@
![4 皇后问题的解](n_queens_problem.assets/solution_4_queens.png)
下图展示了本题的三个约束条件:**多个皇后不能在同一行、同一列、同一条对角线上**。值得注意的是,对角线分为主对角线 `\`次对角线 `/` 两种。
下图展示了本题的三个约束条件:**多个皇后不能在同一行、同一列、同一条对角线上**。值得注意的是,对角线分为 `\``/` 两种方向
![n 皇后问题的约束条件](n_queens_problem.assets/n_queens_constraints.png)
@ -28,7 +28,11 @@
为了满足列约束,我们可以利用一个长度为 $n$ 的布尔型数组 `cols` 记录每一列是否有皇后。在每次决定放置前,我们通过 `cols` 将已有皇后的列进行剪枝,并在回溯中动态更新 `cols` 的状态。
那么,如何处理对角线约束呢?设棋盘中某个格子的行列索引为 $(row, col)$ ,选定矩阵中的某条主对角线,我们发现该对角线上所有格子的行索引减列索引都相等,**即对角线上所有格子的 $row - col$ 为恒定值**。
!!! tip
请注意,矩阵的起点位于左上角,其中行索引从上到下增加,列索引从左到右增加。
那么,如何处理对角线约束呢?设棋盘中某个格子的行列索引为 $(row, col)$ ,选定矩阵中的某条主对角线,我们发现该对角线上所有格子的行索引减列索引都相等,**即主对角线上所有格子的 $row - col$ 为恒定值**。
也就是说,如果两个格子满足 $row_1 - col_1 = row_2 - col_2$ ,则它们一定处在同一条主对角线上。利用该规律,我们可以借助下图所示的数组 `diags1` 记录每条主对角线上是否有皇后。

View File

@ -20,7 +20,7 @@ That is, our contributors are computer scientists, engineers, and students from
- **Native Chinese with professional working English**: Ensuring translation accuracy and consistency between CN and EN versions.
- **Native English**: Enhance the authenticity and fluency of the English content to flow naturally and to be engaging.
Don't hesitate to join us via WeChat `krahets-jyd` or on [Discord](https://discord.gg/9hrbyZFBX3)!
Don't hesitate to join us via WeChat `krahets-jyd` or on [Discord](https://discord.gg/nvspS56295)!
## Translation process