forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrog Position After T Seconds.cpp
59 lines (48 loc) · 1.74 KB
/
Frog Position After T Seconds.cpp
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
56
57
58
59
// Runtime: 29 ms (Top 69.74%) | Memory: 16.4 MB (Top 15.95%)
class Solution {
public:
double frogPosition(int n, vector<vector<int>>& edges, int t, int target) {
unordered_map<int, vector<int>> adjList;
for(const auto& edge : edges) {
adjList[edge[0]].push_back(edge[1]);
adjList[edge[1]].push_back(edge[0]);
}
// BFS way
queue<pair<int, double>> Q;
Q.push({1, 1.0});
int time = 0;
vector<int> visited(n+1, false);
while(not Q.empty()) {
int size = Q.size();
if (time > t) break;
while(size--) {
auto pp = Q.front(); Q.pop();
int node = pp.first;
double prob = pp.second;
visited[node] = true;
// Count the unvisited nbr
int nbrCount = 0;
for(auto& nbr : adjList[node]) {
if (not visited[nbr]) nbrCount++;
}
if (node == target) {
if (time == t) return prob;
if (time < t) {
// Check if any unvisited ? if yes, then frog would jump there and not be able to jump back here
if (nbrCount > 0) return 0.0;
// else return the same prob
return prob;
}
}
for(auto& nbr : adjList[node]) {
if (not visited[nbr]) {
// update the prob as it will be divided by number of nbr
Q.push({nbr, (prob * (1.0/nbrCount))});
}
}
}
time++;
}
return 0.0;
}
};