From c4d7fe5f4ee8698c96370b223be4a9b8d8d929e0 Mon Sep 17 00:00:00 2001 From: tomato <18185657+taoyq1988@users.noreply.github.com> Date: Fri, 27 Sep 2024 17:35:10 +0800 Subject: [PATCH 1/2] feat: add go solution to lcof2 problem: No.109 (#3574) --- .../Solution.go" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/Solution.go" diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/Solution.go" new file mode 100644 index 0000000000000..e150cc42225e8 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/Solution.go" @@ -0,0 +1,36 @@ +func openLock(deadends []string, target string) int { + dead := map[string]bool{} + for _, s := range deadends { + dead[s] = true + } + if dead["0000"] { + return -1 + } + if target == "0000" { + return 0 + } + q := []string{"0000"} + visited := map[string]bool{"0000": true} + step := 0 + for len(q) > 0 { + step++ + size := len(q) + for i := 0; i < size; i++ { + cur := q[0] + q = q[1:] + for j := 0; j < 4; j++ { + for k := -1; k <= 1; k += 2 { + next := cur[:j] + string((cur[j]-'0'+byte(k)+10)%10+'0') + cur[j+1:] + if next == target { + return step + } + if !dead[next] && !visited[next] { + q = append(q, next) + visited[next] = true + } + } + } + } + } + return -1 +} \ No newline at end of file From 649daeb862683cb877853cf9c1bbb610e9350a99 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sat, 28 Sep 2024 13:06:03 +0800 Subject: [PATCH 2/2] Update README.md --- .../README.md" | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/README.md" index 906d152fed407..d4b88475a5771 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/README.md" @@ -236,6 +236,47 @@ public: }; ``` +#### Go + +```go +func openLock(deadends []string, target string) int { + dead := map[string]bool{} + for _, s := range deadends { + dead[s] = true + } + if dead["0000"] { + return -1 + } + if target == "0000" { + return 0 + } + q := []string{"0000"} + visited := map[string]bool{"0000": true} + step := 0 + for len(q) > 0 { + step++ + size := len(q) + for i := 0; i < size; i++ { + cur := q[0] + q = q[1:] + for j := 0; j < 4; j++ { + for k := -1; k <= 1; k += 2 { + next := cur[:j] + string((cur[j]-'0'+byte(k)+10)%10+'0') + cur[j+1:] + if next == target { + return step + } + if !dead[next] && !visited[next] { + q = append(q, next) + visited[next] = true + } + } + } + } + } + return -1 +} +``` +