Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 27171d9

Browse files
committed
feat: bfs for 752 open lock
1 parent f62d6b7 commit 27171d9

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

‎src/752_openLock/algo.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package algo
2+
3+
func openLock(deadends []string, target string) int {
4+
type Node struct {
5+
Number string
6+
Move int
7+
}
8+
visited := map[string]bool{}
9+
q := []Node{{
10+
Number: "0000",
11+
Move: 0,
12+
}}
13+
14+
deadendMap := map[string]bool{}
15+
for _, deadend := range deadends {
16+
deadendMap[deadend] = true
17+
}
18+
if len(deadends) >= 8 && checkDeadlock(deadendMap, target) {
19+
return -1
20+
}
21+
22+
for len(q) > 0 {
23+
// TODO: 什麼時候要+ move
24+
cursor := q[0]
25+
q = q[1:]
26+
visited[cursor.Number] = true
27+
28+
if _, ok := deadendMap[cursor.Number]; ok {
29+
continue
30+
}
31+
if cursor.Number == target {
32+
return cursor.Move
33+
}
34+
35+
neighbors := findNeighbors(cursor.Number)
36+
for _, neighbor := range neighbors {
37+
q = append(q, Node{Number: neighbor, Move: cursor.Move + 1})
38+
}
39+
}
40+
return -1
41+
}
42+
43+
func findNeighbors(cursor string) []string {
44+
neighbors := []string{}
45+
for i := 0; i < len(cursor); i++ {
46+
c := int(cursor[i])
47+
if c == 48 {
48+
neighbors = append(neighbors, cursor[0:i]+"1"+cursor[i+1:])
49+
neighbors = append(neighbors, cursor[0:i]+"9"+cursor[i+1:])
50+
} else {
51+
neighbors = append(neighbors, cursor[0:i]+string(rune(c+1))+cursor[i+1:])
52+
neighbors = append(neighbors, cursor[0:i]+string(rune(c-1))+cursor[i+1:])
53+
}
54+
}
55+
return neighbors
56+
}
57+
58+
func checkDeadlock(deadendMap map[string]bool, target string) bool {
59+
60+
neighbors := findNeighbors(target)
61+
for _, neighbor := range neighbors {
62+
if _, ok := deadendMap[neighbor]; !ok {
63+
return false
64+
}
65+
}
66+
return true
67+
}

‎src/752_openLock/algo_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package algo
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestOpenLock(t *testing.T) {
11+
deadends := []string{"0201", "0101", "0102", "1212", "2002"}
12+
actual := openLock(deadends, "0202")
13+
fmt.Println("actual: ", actual)
14+
assert.Equal(t, 6, actual)
15+
}

‎src/752_openLock/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module algo
2+
3+
go 1.18
4+
5+
require github.com/stretchr/testify v1.8.4
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

‎src/752_openLock/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
6+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /