1730. Shortest Path to Get Food

You are starving and you want to eat food as quickly as possible. You want to find the shortest path to arrive at any food cell.

You are given an m x n character matrix, grid, of these different types of cells:

  • '*' is your location. There is exactly one '*' cell.
  • '#' is a food cell. There may be multiple food cells.
  • 'O' is free space, and you can travel through these cells.
  • 'X' is an obstacle, and you cannot travel through these cells.

You can travel to any adjacent cell north, east, south, or west of your current location if there is not an obstacle.

Return the length of the shortest path for you to reach any food cell. If there is no path for you to reach food, return -1.

 

Example 1:

Input: grid = [["X","X","X","X","X","X"],["X","*","O","O","O","X"],["X","O","O","#","O","X"],["X","X","X","X","X","X"]]
Output: 3
Explanation: It takes 3 steps to reach the food.

Example 2:

Input: grid = [["X","X","X","X","X"],["X","*","X","O","X"],["X","O","X","#","X"],["X","X","X","X","X"]]
Output: -1
Explanation: It is not possible to reach the food.

Example 3:

Input: grid = [["X","X","X","X","X","X","X","X"],["X","*","O","X","O","#","O","X"],["X","O","O","X","O","O","X","X"],["X","O","O","O","O","#","O","X"],["X","X","X","X","X","X","X","X"]]
Output: 6
Explanation: There can be multiple food cells. It only takes 6 steps to reach the bottom food.

Example 4:

Input: grid = [["O","*"],["#","O"]]
Output: 2

Example 5:

Input: grid = [["X","*"],["#","X"]]
Output: -1

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 200
  • grid[row][col] is '*', 'X', 'O', or '#'.
  • The grid contains exactly one '*'.

Rust Solution

struct Solution;

use std::collections::VecDeque;

impl Solution {
    fn get_food(grid: Vec<Vec<char>>) -> i32 {
        let n = grid.len();
        let m = grid[0].len();
        let mut queue: VecDeque<(usize, usize, i32)> = VecDeque::new();
        let mut visited: Vec<Vec<bool>> = vec![vec![false; m]; n];
        'outer: for i in 0..n {
            for j in 0..m {
                if grid[i][j] == '*' {
                    queue.push_back((i, j, 0));
                    visited[i][j] = true;
                    break 'outer;
                }
            }
        }
        while let Some((i, j, d)) = queue.pop_front() {
            for (di, dj) in &[(1, 0), (-1, 0), (0, 1), (0, -1)] {
                if grid[i][j] == '#' {
                    return d;
                }
                let i = i as i32 + di;
                let j = j as i32 + dj;
                if 0 <= i && i < n as i32 && 0 <= j && j < m as i32 {
                    let i = i as usize;
                    let j = j as usize;
                    if !visited[i][j] && grid[i][j] != 'X' {
                        queue.push_back((i, j, d + 1));
                        visited[i][j] = true;
                    }
                }
            }
        }
        -1
    }
}

#[test]
fn test() {
    let grid = vec_vec_char![
        ['X', 'X', 'X', 'X', 'X', 'X'],
        ['X', '*', 'O', 'O', 'O', 'X'],
        ['X', 'O', 'O', '#', 'O', 'X'],
        ['X', 'X', 'X', 'X', 'X', 'X']
    ];
    let res = 3;
    assert_eq!(Solution::get_food(grid), res);
    let grid = vec_vec_char![
        ['X', 'X', 'X', 'X', 'X'],
        ['X', '*', 'X', 'O', 'X'],
        ['X', 'O', 'X', '#', 'X'],
        ['X', 'X', 'X', 'X', 'X']
    ];
    let res = -1;
    assert_eq!(Solution::get_food(grid), res);
    let grid = vec_vec_char![
        ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'],
        ['X', '*', 'O', 'X', 'O', '#', 'O', 'X'],
        ['X', 'O', 'O', 'X', 'O', 'O', 'X', 'X'],
        ['X', 'O', 'O', 'O', 'O', '#', 'O', 'X'],
        ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X']
    ];
    let res = 6;
    assert_eq!(Solution::get_food(grid), res);
    let grid = vec_vec_char![['O', '*'], ['#', 'O']];
    let res = 2;
    assert_eq!(Solution::get_food(grid), res);
    let grid = vec_vec_char![['X', '*'], ['#', 'X']];
    let res = -1;
    assert_eq!(Solution::get_food(grid), res);
}

Having problems with this solution? Click here to submit an issue on github.