214. Shortest Path to Get Food

Given an m*n grid where each cell can be one of the following:

  • * represents your starting position.There is exactly one * cell.
  • # represents a food location.
  • O represents an empty space, and you can move through these cells.
  • X represents an obstacle, and you cannot pass through it.

You can move up, down, left, or right and you must find the shortest path to reach a food cell (#). If there is no possible path, 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.

Now Your Turn!

Pick the correct output for the given input

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"]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int getFood(vector<vector<char>>& grid) {
//Your Code Goes Here
}
};
Test Case

Input:

Grid