LeetCode 1730: Shortest Path to Get Food Solution

Master LeetCode problem 1730 (Shortest Path to Get Food), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1730. Shortest Path to Get Food

Problem Explanation

Explanation:

  • Algorithmic Idea:

    1. Use Breadth First Search (BFS) to find the shortest path to reach the food.
    2. Start the search from the position of the 'S' character (starting point).
    3. Explore all possible directions (up, down, left, right) from the current position.
    4. Keep track of the steps taken to reach each cell and update the minimum steps whenever the food ('*') cell is reached.
    5. Return the minimum steps if the food is reachable, otherwise return -1.
  • Step-by-Step Iteration:

    • Start BFS from the starting position 'S'.
    • Explore all possible directions from the current position.
    • Keep track of the minimum steps needed to reach the food.
    • Continue exploring until the food is reached or no valid paths left.
  • Time Complexity: O(N), where N is the total number of cells in the grid.

  • Space Complexity: O(N) for the queue and visited set.

:

Solution Code

class Solution {
    public int getFood(char[][] grid) {
        int rows = grid.length;
        int cols = grid[0].length;
        
        Queue<int[]> queue = new LinkedList<>();
        Set<String> visited = new HashSet<>();
        int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (grid[i][j] == 'S') {
                    queue.offer(new int[]{i, j, 0});
                    visited.add(i + "," + j);
                }
            }
        }
        
        while (!queue.isEmpty()) {
            int[] curr = queue.poll();
            int x = curr[0];
            int y = curr[1];
            int steps = curr[2];
            
            if (grid[x][y] == '*') {
                return steps;
            }
            
            for (int[] dir : dirs) {
                int newX = x + dir[0];
                int newY = y + dir[1];
                
                if (newX >= 0 && newX < rows && newY >= 0 && newY < cols && grid[newX][newY] != 'X' && !visited.contains(newX + "," + newY)) {
                    queue.offer(new int[]{newX, newY, steps + 1});
                    visited.add(newX + "," + newY);
                }
            }
        }
        
        return -1;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1730 (Shortest Path to Get Food)?

This page provides optimized solutions for LeetCode problem 1730 (Shortest Path to Get Food) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1730 (Shortest Path to Get Food)?

The time complexity for LeetCode 1730 (Shortest Path to Get Food) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1730 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1730 in Java, C++, or Python.

Back to LeetCode Solutions