Frog Position After T Seconds

Given an undirected tree consisting of n vertices numbered from 1 to n. A frog starts jumping from the vertex 1. In one second, the frog jumps from its current vertex to another unvisited vertex if they are directly connected. The frog can not jump back to a visited vertex. In case the frog can jump to several vertices it jumps randomly to one of them with the same probability, otherwise, when the frog can not jump to any unvisited vertex it jumps forever on the same vertex. 

The edges of the undirected tree are given in the array edges, where edges[i] = [fromi, toi] means that exists an edge connecting directly the vertices fromi and toi.

Return the probability that after t seconds the frog is on the vertex target. 

Example 1:

Frog Position After T Seconds_第1张图片

Input: n = 7, edges = [[1,2],[1,3],[1,7],[2,4],[2,6],[3,5]], t = 2, target = 4
Output: 0.16666666666666666 
Explanation: The figure above shows the given graph. The frog starts at vertex 1, jumping with 1/3 probability to the vertex 2 after second 1 and then jumping with 1/2 probability to vertex 4 after second 2. Thus the probability for the frog is on the vertex 4 after 2 seconds is 1/3 * 1/2 = 1/6 = 0.16666666666666666. 

思路:这题最主要的是知道,frog会继续往下跳,如果还有下一层,而且t-- >0 的时候。也就是说,即使现在是在target点上,不一定之后还是在这个node,frog会继续跳,那么巧妙的是用一个pos array去记录到达每个node的pos;如果还有下一层,当前层的所有node possiblity全部清空为0;为了更好的记录pos,申请一个[n + 1] 的数组;

class Solution {
    public double frogPosition(int n, int[][] edges, int t, int target) {
        HashMap> graph = new HashMap<>();
        for(int i = 1; i <= n; i++) {
            graph.putIfAbsent(i, new ArrayList());
        }
        for(int[] e: edges) {
            int from = e[0];
            int to = e[1];
            graph.get(from).add(to);
            graph.get(to).add(from);
        }
        Queue queue = new LinkedList();
        double[] pos = new double[n + 1];
        HashSet visited = new HashSet<>();
        queue.offer(1);
        visited.add(1);
        pos[1] = 1.0;
        
        while(!queue.isEmpty() && t-- > 0) {
            int size = queue.size();
            for(int i = 0; i < size; i++) {
                Integer node = queue.poll();
                int nextlevelnodes = 0;
                // 除去parent,接下来的children有多少;
                for(Integer neighbor: graph.get(node)) {
                    if(!visited.contains(neighbor)) {
                        nextlevelnodes++;
                    }
                }
                for(Integer neighbor: graph.get(node)) {
                    if(!visited.contains(neighbor)) {
                        visited.add(neighbor);
                        queue.offer(neighbor);
                        pos[neighbor] = pos[node] * (1.0 / nextlevelnodes);
                    }
                }
                // 如果还有下一层,frog会继续jump,不会停止;所以,上一层的node pos全部变成0;
                if(nextlevelnodes > 0) {
                    pos[node] = 0;
                }
            }
        }
        return pos[target];
    }
}

 

你可能感兴趣的:(BFS)