Skip to content

Commit b759223

Browse files
committed
回溯: n皇后加注释
Change-Id: I380e0a1d0b55e0c30a26996ed817c4a637939bbb
1 parent 919c18e commit b759223

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

go/leetcode/51.N皇后.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,21 @@ func dfs(rect *[][]string, row int, res *[][]string) {
8282
}
8383

8484
func isValid(rect *[][]string, row int, col int) bool {
85+
// 因为每次都是递增row和col,所以之后的row和col不需要检查
86+
// 不需要检查行,以为每行只有一次放的机会
87+
// 检测列
8588
for i := 0; i < row; i++ {
8689
if (*rect)[i][col] == "Q" {
8790
return false
8891
}
8992
}
93+
// 左上对角线检查
9094
for i, j := row - 1, col - 1; i >= 0 && j >= 0; i, j = i-1, j-1 {
9195
if (*rect)[i][j] == "Q" {
9296
return false
9397
}
9498
}
99+
// 右上对角线检查
95100
for i, j := row - 1, col + 1; i >= 0 && j < len(*rect); i, j = i-1, j+1 {
96101
if (*rect)[i][j] == "Q" {
97102
return false

0 commit comments

Comments
 (0)