-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1377.T 秒后青蛙的位置.js
45 lines (44 loc) · 956 Bytes
/
1377.T 秒后青蛙的位置.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
/**
* @param {number} n
* @param {number[][]} edges
* @param {number} t
* @param {number} target
* @return {number}
*/
var frogPosition = function(n, edges, t, target) {
const map = new Map();
for (let [a, b] of edges) {
if (b < a) {
const t = a;
a = b;
b = t;
}
if (map.get(a)) {
map.get(a).push(b);
} else {
map.set(a, [b]);
}
}
let result = 0;
function dfs(t, cur, r) {
if (result) {
return;
}
if (t === 0) {
if (cur === target) {
result = r;
}
return;
}
const child = map.get(cur) || [];
if (child.length) {
for (const i of child) {
dfs(t - 1, i, r / child.length);
}
} else {
dfs(t - 1, cur, r);
}
}
dfs(t, 1, 1);
return result;
};