-
Notifications
You must be signed in to change notification settings - Fork 0
/
695.岛屿的最大面积.rs
98 lines (86 loc) · 1.98 KB
/
695.岛屿的最大面积.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#![feature(trace_macros)]
struct Solution;
macro_rules! vec2d {
($($arr:tt),*) => { // handle sets
{
let mut ret= Vec::new();
$(ret.push(vec!$arr);)*
ret
}
};
}
use std::cmp;
/*
* @lc app=leetcode.cn id=695 lang=rust
*
* [695] 岛屿的最大面积
*/
impl Solution {
fn dfs(grid: &mut Vec<Vec<i32>>, x: usize, y: usize) -> i32 {
println!("[{},{}]", x, y);
if x >= grid.len() || y >= grid[0].len() {
return 0;
}
if grid[x][y] == 0 {
return 0;
}
grid[x][y] = 0; // mark visited
1 + if x >= 1 {
Solution::dfs(grid, x - 1, y)
} else {
0
} + Solution::dfs(grid, x + 1, y)
+ if y >= 1 {
Solution::dfs(grid, x, y - 1)
} else {
0
}
+ Solution::dfs(grid, x, y + 1)
}
pub fn max_area_of_island(grid: Vec<Vec<i32>>) -> i32 {
// let grid = grid as Vec<Vec<usize>>;
let mut grid = grid.clone();
let height = grid.len();
let width = grid[0].len();
let mut max = 0;
for i in 0..height {
for j in 0..width {
max = cmp::max(max, Solution::dfs(&mut grid, i, j));
}
}
max
}
}
#[test]
fn case() {
assert_eq!(
Solution::contains_nearby_duplicate(vec![1, 2, 3, 1], 3),
true
);
assert_eq!(
Solution::contains_nearby_duplicate(vec![1, 0, 1, 1], 1),
true
);
assert_eq!(
Solution::contains_nearby_duplicate(vec![1, 2, 3, 1, 2, 3], 2),
false
);
}
fn main() {
// let sol = Solution;"jjhafiecg"
// "gj"
// trace_macros!(true);
println!(
"{}",
Solution::max_area_of_island(vec2d![
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
])
);
}