Fetch the repository succeeded.
package openlock
import "fmt"
func openLock(deadends []string, target string) int {
deads := make(map[string]int)
for _, v := range deadends {
deads[v]++
}
queue := make([]string, 0, 0)
queue = append(queue, "0000")
step := 0
for len(queue) != 0 {
size := len(queue)
for i := 0; i < size; i++ {
// 出队
top := queue[0]
queue = queue[1:]
if _, dead := deads[top]; dead {
continue
}
if top == target {
return step
}
fmt.Println(top)
deads[top]++
for j := 0; j < 4; j++ {
nodeUp := clickUp(top, j)
nodeDown := clickDown(top, j)
queue = append(queue, nodeUp, nodeDown)
}
}
step++
}
return -1
}
func clickUp(code string, idx int) string {
decodes := []byte(code)
ch := decodes[idx]
if ch == '9' {
decodes[idx] = '0'
return string(decodes)
}
ch++
decodes[idx] = ch
return string(decodes)
}
func clickDown(code string, idx int) string {
decodes := []byte(code)
ch := decodes[idx]
if ch == '0' {
decodes[idx] = '9'
return string(decodes)
}
ch--
decodes[idx] = ch
return string(decodes)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。