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

Commit 3a377a1

Browse files
aQuaaQua
authored andcommitted
773 finish
1 parent a4327d8 commit 3a377a1

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Algorithms/0773.sliding-puzzle/sliding-puzzle.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ import (
55
)
66

77
func slidingPuzzle(board [][]int) int {
8-
original, target := "123450", convert(board)
9-
if original == target {
8+
origin, target := "123450", convert(board)
9+
if origin == target {
1010
return 0
1111
}
1212

13-
q := make([]string, 1, 720)
14-
q[0] = original
13+
queue := make([]string, 1, 720)
14+
queue[0] = origin
1515

1616
hasSeen := make(map[string]bool, 720)
17-
hasSeen[original] = true
17+
hasSeen[origin] = true
1818

19-
res := 0 // q 中最后一个候选项是 origin 经过 res 次变化得来的
19+
res := 0
2020
d := []int{-1, 1, 3, -3}
21-
size := len(q)
22-
for len(q) > 0 {
23-
//
24-
s := q[0]
21+
countDown := len(queue)
22+
for len(queue) > 0 {
23+
// 从队列中抽取 s 对其进行变形
24+
s := queue[0]
2525
i := strings.IndexByte(s, '0')
2626
for k := range d {
2727
j := i + d[k]
@@ -37,15 +37,16 @@ func slidingPuzzle(board [][]int) int {
3737
}
3838

3939
if !hasSeen[c] {
40-
q = append(q, c)
40+
queue = append(queue, c)
4141
hasSeen[c] = true
4242
}
4343
}
4444

45-
q = q[1:]
46-
size--
47-
if size == 0 {
48-
size = len(q)
45+
// 删除队列头部,检查 res 是否需要 ++
46+
queue = queue[1:]
47+
countDown--
48+
if countDown == 0 {
49+
countDown = len(queue)
4950
res++
5051
}
5152
}

0 commit comments

Comments
 (0)