-
Notifications
You must be signed in to change notification settings - Fork 0
/
pond_sizes_lcci.rs
54 lines (46 loc) · 1.44 KB
/
pond_sizes_lcci.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
// 水域大小
// https://leetcode.cn/problems/pond-sizes-lcci
// INLINE ../../images/search/pond_sizes_lcci.jpeg
pub struct Solution;
impl Solution {
pub fn pond_sizes(mut land: Vec<Vec<i32>>) -> Vec<i32> {
let m = land.len();
let n = land[0].len();
// 定义深度优先搜索函数
fn dfs(land: &mut Vec<Vec<i32>>, x: usize, y: usize) -> i32 {
let m = land.len();
let n = land[0].len();
// 如果越界或已经访问过,返回0
if x >= m || y >= n || land[x][y] != 0 {
return 0;
}
// 标记为已访问
land[x][y] = -1;
let mut res = 1;
// 递归搜索8个方向
for dx in -1..=1 {
for dy in -1..=1 {
if dx == 0 && dy == 0 {
continue;
}
let x_new = (x as i32) + dx;
let y_new = (y as i32) + dy;
if x_new >= 0 && y_new >= 0 {
res += dfs(land, x_new as usize, y_new as usize);
}
}
}
res
}
let mut res = Vec::new();
for i in 0..m {
for j in 0..n {
if land[i][j] == 0 {
res.push(dfs(&mut land, i, j));
}
}
}
res.sort_unstable();
res
}
}