-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path16928.js
55 lines (45 loc) · 1.28 KB
/
16928.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var readline = require("readline");
var r = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let input = [];
r.on("line", function (line) {
input.push(line);
}).on("close", function () {
main(input);
process.exit();
});
function main(input) {
const [N, M] = input.shift().split(' ').map(Number)
const graph = []
input.forEach(element => {
const [from, to] = element.split(' ').map(Number)
graph[from] = to
});
function bfs() {
let queue = [1];
let tempqueue = [];
let depth = 0;
let visited = [true, true]
while (1) {
depth++
while (queue.length !== 0) {
let node = queue.pop()
for (let i = 1; i <= 6; i++) {
if (graph[i + node] === 100 || node + i == 100) return depth;
if (graph[i + node]) {
tempqueue.push(graph[i + node])
}
else if (!visited[i + node]) {
tempqueue.push(node + i)
visited[node + i] = true
}
}
}
queue = [...tempqueue]
tempqueue = []
}
}
console.log(bfs())
}