Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
fix #57
Browse files Browse the repository at this point in the history
  • Loading branch information
aQuaYi committed Jan 11, 2020
1 parent 88a5378 commit 4fdbed1
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Algorithms/0051.n-queens/n-queens.go
Expand Up @@ -32,8 +32,27 @@ func dfs(r int, cols, d1, d2 []bool, board []string, res *[][]string) {
n := len(board)

for c := 0; c < len(board); c++ {
// 把棋盘想象成
// 以左上角为坐标原点 [0,0]
// C 轴正方向向右
// R 轴正方向向下
// 的坐标系。
// 这样的话,每个格子就都有了自己的坐标值 [c,r]
//
// 对于 '\' 方向的斜线而言
// 同一个斜线上的格子,利用其坐标 [c,r] 计算 r-c 的结果相同,
// 不同斜线上 r-c 的结果不同。
// 所以可以用 r-c 代表不同的 '\' 方向的斜线的编号。
// 但是 r-c 有可能是负值,无法作为切片的索引值
// 所以 +n,可知 r-c+n >= 0
// 所以,使用 r-c+n 作为 '\' 方向斜线切片的索引值。
// 对于 '/' 方向的斜线而言
// 同一个斜线上的格子,利用其坐标 [c,r] 计算 r+c 的结果相同,
// 不同斜线上 r+c 的结果不同。
// 所以可以用 r+c 代表不同的 '/' 方向的斜线的编号。
// 所以,使用 r+c 作为 '/' 方向斜线切片的索引值。
id1 := r - c + n
id2 := 2*n - r - c - 1
id2 := r + c
if !cols[c] && !d1[id1] && !d2[id2] {
b := make([]byte, n)
for i := range b {
Expand Down

0 comments on commit 4fdbed1

Please sign in to comment.