-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrotting-oranges.rs
64 lines (56 loc) · 1.72 KB
/
rotting-oranges.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
#![allow(dead_code, unused, unused_variables, non_snake_case)]
fn main() {}
struct Solution;
impl Solution {
pub fn oranges_rotting(grid: Vec<Vec<i32>>) -> i32 {
let mut hash = std::collections::HashSet::new();
let mut stack = vec![];
for i in 0..grid.len() {
for j in 0..grid[0].len() {
match grid[i][j] {
1 => {
hash.insert((i, j));
}
2 => stack.push((i, j)),
_ => {}
}
}
}
let mut result = 0;
while !stack.is_empty() {
let mut new_stack = vec![];
let len = hash.len();
while let Some((x, y)) = stack.pop() {
if x > 0 {
if hash.contains(&(x - 1, y)) {
new_stack.push((x - 1, y));
hash.remove(&(x - 1, y));
}
}
if y > 0 {
if hash.contains(&(x, y - 1)) {
new_stack.push((x, y - 1));
hash.remove(&(x, y - 1));
}
}
if hash.contains(&(x + 1, y)) {
new_stack.push((x + 1, y));
hash.remove(&(x + 1, y));
}
if hash.contains(&(x, y + 1)) {
new_stack.push((x, y + 1));
hash.remove(&(x, y + 1));
}
}
if hash.len() != len {
result += 1;
}
stack = new_stack;
}
if hash.is_empty() {
result
} else {
-1
}
}
}