forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2.cpp
More file actions
32 lines (32 loc) · 999 Bytes
/
s2.cpp
File metadata and controls
32 lines (32 loc) · 999 Bytes
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
// OJ: https://leetcode.com/problems/frog-position-after-t-seconds/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
// Ref: https://leetcode.com/problems/frog-position-after-t-seconds/discuss/532571/Python-DFS
class Solution {
vector<vector<int>> g;
vector<bool> seen;
double dfs(int node, int t, int target) {
seen[node] = true;
int cnt = g[node].size() - (node != 1);
if (!t) return node == target;
if (node == target) return !cnt;
double ans = 0;
for (int n : g[node]) {
if (seen[n]) continue;
if (ans = dfs(n, t - 1, target)) break;
}
if (ans) ans /= cnt;
return ans;
}
public:
double frogPosition(int n, vector<vector<int>>& edges, int t, int target) {
g.resize(n + 1);
seen.resize(n + 1);
for (auto &e : edges) {
g[e[0]].push_back(e[1]);
g[e[1]].push_back(e[0]);
}
return dfs(1, t, target);
}
};