1
use std::collections::{HashSet, VecDeque};3
const DIRECTIONS: [(isize, isize); 4] = [(1, 0), (0, 1), (-1, 0), (0, -1)];5
pub fn oranges_rotting(grid: Vec<Vec<i32>>) -> i32 {6
let mut queue = VecDeque::new();7
let mut seen = HashSet::new();10
// fill the queue with all rotten oranges11
for i in 0..grid.len() {12
for j in 0..grid[0].len() {14
queue.push_back((i as isize, j as isize, 0));20
// perform bfs and spread rotting orange21
while let Some((i, j, time)) = queue.pop_front() {22
for &(di, dj) in &DIRECTIONS {28
&& next_i < grid.len() as isize29
&& next_j < grid[0].len() as isize30
&& !seen.contains(&(next_i as usize, next_j as usize))31
&& grid[next_i as usize][next_j as usize] == 133
queue.push_back((next_i, next_j, time + 1));34
seen.insert((next_i as usize, next_j as usize));40
// make sure all oranges are rotted; if any aren't then its impossible to rot all oranges41
for i in 0..grid.len() {42
for j in 0..grid[0].len() {43
if grid[i][j] == 1 && !seen.contains(&(i, j)) {