forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.rs
63 lines (62 loc) · 1.88 KB
/
Solution.rs
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
60
61
62
63
use std::collections::VecDeque;
impl Solution {
fn dfs(i: usize, j: usize, v: i32, g: &Vec<Vec<i32>>, vis: &mut Vec<Vec<bool>>) -> bool {
if vis[i][j] || g[i][j] <= v {
return false;
}
vis[i][j] = true;
let n = g.len();
(i == n - 1 && j == n - 1)
|| (i != 0 && Self::dfs(i - 1, j, v, g, vis))
|| (i != n - 1 && Self::dfs(i + 1, j, v, g, vis))
|| (j != 0 && Self::dfs(i, j - 1, v, g, vis))
|| (j != n - 1 && Self::dfs(i, j + 1, v, g, vis))
}
pub fn maximum_safeness_factor(grid: Vec<Vec<i32>>) -> i32 {
let n = grid.len();
let mut g = vec![vec![-1; n]; n];
let mut q = VecDeque::new();
for i in 0..n {
for j in 0..n {
if grid[i][j] == 1 {
q.push_back((i, j));
}
}
}
let mut level = 0;
while !q.is_empty() {
let m = q.len();
for _ in 0..m {
let (i, j) = q.pop_front().unwrap();
if g[i][j] != -1 {
continue;
}
g[i][j] = level;
if i != n - 1 {
q.push_back((i + 1, j));
}
if i != 0 {
q.push_back((i - 1, j));
}
if j != n - 1 {
q.push_back((i, j + 1));
}
if j != 0 {
q.push_back((i, j - 1));
}
}
level += 1;
}
let mut left = 0;
let mut right = level;
while left < right {
let mid = (left + right) >> 1;
if Self::dfs(0, 0, mid, &g, &mut vec![vec![false; n]; n]) {
left = mid + 1;
} else {
right = mid;
}
}
right
}
}