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  // @Title: 地图中的最高点 (Map of Highest Peak) // @Author: 15816537946@163.com // @Date: 2022-01-29 23:26:03 // @Runtime: 96 ms // @Memory: 14 MB use std::collections::VecDeque; impl Solution { pub fn highest_peak(is_water: Vec>) -> Vec> { let directions = vec![(0, 1), (0, -1), (1, 0), (-1, 0)]; let m = is_water.len(); let n = is_water[0].len(); let mut height = vec![vec![-1; n]; m]; let mut deque = VecDeque::new(); for i in 0..m { for j in 0..n { if is_water[i][j] == 1 { height[i][j] = 0; deque.push_back((i, j, 0)); } } } while !deque.is_empty() { let (i, j, h) = deque.pop_front().unwrap(); for &(di, dj) in directions.iter() { let i = i as i32 + di; let j = j as i32 + dj; if i < 0 || i >= m as i32 || j < 0 || j >= n as i32 { continue; } let i = i as usize; let j = j as usize; if height[i][j] != -1 { continue; } height[i][j] = h + 1; deque.push_back((i, j, h + 1)); } } height } }