-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.go
69 lines (60 loc) · 1.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
func openLock(deadends []string, target string) int {
dead := make(map[string]bool)
for _, deadend := range deadends {
if deadend == "0000" {
return -1
}
dead[deadend] = true
}
used := map[string]bool{"0000": true}
queue := [][]interface{}{{"0000", 0}}
for len(queue) > 0 {
item := queue[0]
queue = queue[1:]
status, cnt := item[0].(string), item[1].(int)
if status == target {
return cnt
}
nextStatus := bfs(status, used)
for _, s := range nextStatus {
if used[s] || dead[s] {
continue
}
if s == target {
return cnt + 1
}
used[s] = true
queue = append(queue, []interface{}{
s, cnt + 1,
})
}
}
return -1
}
func bfs(status string, used map[string]bool) (ret []string) {
for i := 0; i < 4; i++ {
orig := status[i]
bs := []byte(status)
var next byte
if orig == '9' {
next = '0'
} else {
next = orig + 1
}
bs[i] = next
if tmp := string(bs); !used[tmp] {
ret = append(ret, tmp)
}
if orig == '0' {
next = '9'
} else {
next = orig - 1
}
bs[i] = next
if tmp := string(bs); !used[tmp] {
ret = append(ret, tmp)
}
}
return
}