-
Notifications
You must be signed in to change notification settings - Fork 0
/
open-the-lock.js
38 lines (35 loc) · 1.04 KB
/
open-the-lock.js
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
/**
* @param {string[]} deadends
* @param {string} target
* @return {number}
*/
var openLock = function(deadends, target) {
var calculateNext = function(char, mov){
if(char == '0' && mov == -1) return '9'
if(char == '9' && mov == 1) return '1'
return String.fromCharCode(char.charCodeAt(0) + mov);
}
let queue = ['0000']
let visited = new Set(['0000'])
let turns = 0
const movs = [1,-1]
while(queue.length){
const next = []
for(let node of queue){
if(node === target) return turns
if(deadends.includes(node)) continue
for(let i = 0; i < 4; i++){
movs.forEach(mov => {
let newNode = node.slice(0, i) + calculateNext(node[i], mov) + node.slice(i+1, 4);
if(!visited.has(newNode)){
visited.add(newNode)
next.push(newNode)
}
})
}
}
turns++
queue = next
}
return -1
};