Skip to content

Commit

Permalink
Create 24-frog-position-after-t-seconds.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed May 24, 2023
1 parent 98b7a46 commit 7c1eb8a
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 2023/05/24-frog-position-after-t-seconds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// https://leetcode.cn/problems/frog-position-after-t-seconds/

impl Solution {
pub fn frog_position(n: i32, edges: Vec<Vec<i32>>, t: i32, target: i32) -> f64 {
let mut g = vec![vec![]; n as usize + 1];
for edge in edges {
g[edge[0] as usize].push(edge[1] as usize);
g[edge[1] as usize].push(edge[0] as usize);
}
let mut vis = vec![false; n as usize + 1];
vis[1] = true;
fn dfs(
edges: &Vec<Vec<usize>>,
pos: usize,
probability: f64,
vis: &mut Vec<bool>,
t: i32,
target: i32,
) -> f64 {
if t == 0 {
if pos as i32 == target {
return probability;
} else {
return 0.0;
}
}
let cnt = edges[pos]
.iter()
.filter(|&x| !vis[*x])
.count();
if cnt == 0 {
if pos as i32 == target {
return probability;
} else {
return 0.0;
}
}
let mut ans = 0.0;
for &next in edges[pos].iter() {
if !vis[next] {
vis[next] = true;
ans += dfs(edges, next, probability / cnt as f64, vis, t - 1, target);
vis[next] = false;
}
}
ans
}
dfs(&g, 1, 1.0, &mut vis, t, target)
}
}

0 comments on commit 7c1eb8a

Please sign in to comment.