Approach
Breadth-first search
For Frog Position After T Seconds, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 39 lines of C++ from the credited upstream file 1377.cpp.
- The implementation visibly relies on sequence storage, work queue.
- 4 loop blocks detected.
Complexity
Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public:3 double frogPosition(int n, vector<vector<int>>& edges, int t, int target) {4 vector<vector<int>> tree(n + 1);5 queue<int> q{{1}};6 vector<bool> seen(n + 1);7 vector<double> prob(n + 1);8 9 seen[1] = true;10 prob[1] = 1.0;11 12 for (const vector<int>& edge : edges) {13 const int u = edge[0];14 const int v = edge[1];15 tree[u].push_back(v);16 tree[v].push_back(u);17 }18 19 while (!q.empty() && t-- > 0)20 for (int sz = q.size(); sz > 0; --sz) {21 const int a = q.front();22 q.pop();23 const int nChildren =24 ranges::count_if(tree[a], [&seen](int b) { return !seen[b]; });25 for (const int b : tree[a]) {26 if (seen[b])27 continue;28 seen[b] = true;29 prob[b] = prob[a] / nChildren;30 q.push(b);31 }32 if (nChildren > 0)33 prob[a] = 0.0;34 }35 36 return prob[target];37 }38};39