1091. Shortest Path in Binary Matrix Medium
1/**
2 * [1091] Shortest Path in Binary Matrix
3 *
4 * Given an n x n binary matrix grid, return the length of the shortest clear path in the matrix. If there is no clear path, return -1.
5 * A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0)) to the bottom-right cell (i.e., (n - 1, n - 1)) such that:
6 *
7 * All the visited cells of the path are 0.
8 * All the adjacent cells of the path are 8-directionally connected (i.e., they are different and they share an edge or a corner).
9 *
10 * The length of a clear path is the number of visited cells of this path.
11 *
12 * Example 1:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example1_1.png" style="width: 500px; height: 234px;" />
14 * Input: grid = [[0,1],[1,0]]
15 * Output: 2
16 *
17 * Example 2:
18 * <img alt="" src="https://assets.leetcode.com/uploads/2021/02/18/example2_1.png" style="height: 216px; width: 500px;" />
19 * Input: grid = [[0,0,0],[1,1,0],[1,1,0]]
20 * Output: 4
21 *
22 * Example 3:
23 *
24 * Input: grid = [[1,0,0],[1,1,0],[1,1,0]]
25 * Output: -1
26 *
27 *
28 * Constraints:
29 *
30 * n == grid.length
31 * n == grid[i].length
32 * 1 <= n <= 100
33 * grid[i][j] is 0 or 1
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/shortest-path-in-binary-matrix/
39// discuss: https://leetcode.com/problems/shortest-path-in-binary-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn shortest_path_binary_matrix(grid: Vec<Vec<i32>>) -> i32 {
45 0
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_1091() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.