-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathshortest_path_in_binary_matrix.rs
More file actions
63 lines (52 loc) · 1.92 KB
/
shortest_path_in_binary_matrix.rs
File metadata and controls
63 lines (52 loc) · 1.92 KB
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
#[allow(dead_code)]
struct Solution;
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashSet};
fn neighbours(grid: &[Vec<i32>], row: usize, col: usize) -> Vec<(usize, usize)> {
let (rows, cols) = (grid.len(), grid[0].len());
let mut res = Vec::new();
for (neigh_row_opt, neigh_col_opt) in [
(Some(row + 1), Some(col)),
(Some(row), Some(col + 1)),
(row.checked_sub(1), Some(col)),
(Some(row), col.checked_sub(1)),
(row.checked_sub(1), Some(col + 1)),
(Some(row + 1), Some(col + 1)),
(row.checked_add(1), col.checked_sub(1)),
(row.checked_sub(1), col.checked_sub(1)),
] {
if let (Some(neigh_row), Some(neigh_col)) = (neigh_row_opt, neigh_col_opt) {
if neigh_row < rows && neigh_col < cols && grid[neigh_row][neigh_col] == 0 {
res.push((neigh_row, neigh_col));
}
}
}
res
}
impl Solution {
#[allow(dead_code, clippy::needless_pass_by_value)]
pub fn shortest_path_binary_matrix(grid: Vec<Vec<i32>>) -> i32 {
if grid[0][0] == 1 {
return -1;
}
let mut heap: BinaryHeap<(Reverse<usize>, usize, usize)> = BinaryHeap::new();
let mut visited = HashSet::new();
let (rows, cols) = (grid.len(), grid[0].len());
heap.push((Reverse(1), 0, 0));
visited.insert((0, 0));
while !heap.is_empty() {
let (Reverse(distance), row, col) = heap.pop().unwrap();
if (row, col) == (rows - 1, cols - 1) {
return distance.try_into().unwrap();
}
for (neigh_row, neigh_col) in neighbours(&grid, row, col) {
if visited.contains(&(neigh_row, neigh_col)) {
continue;
}
heap.push((Reverse(distance + 1), neigh_row, neigh_col));
visited.insert((neigh_row, neigh_col));
}
}
-1
}
}