Tutorialspoint
Problem
Solution
Submissions

Shortest Path in a Grid

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to find the shortest path from the top-left corner to the bottom-right corner of a grid, where you are allowed to eliminate at most k obstacles. The grid is represented as a 2D array of 0s and 1s, where 0 represents an empty cell and 1 represents an obstacle. You can move up, down, left, or right from a cell. The length of a path is the number of steps you take.

Example 1
  • Input: grid = [ [0, 0, 0], [1, 1, 0], [0, 0, 0], [0, 1, 1], [0, 0, 0] ] k = 1
  • Output: 6
  • Explanation: Step 1: The grid has 5 rows and 3 columns with obstacles at positions (1,0), (1,1), (3,1), and (3,2). Step 2: We can eliminate at most 1 obstacle. Step 3: One possible path is: (0,0) → (1,0) → (2,0) → (3,0) → (4,0) → (4,1) → (4,2). Step 4: This path encounters 1 obstacle at (1,0) which we eliminate. Step 5: The length of this path is 6 steps. Step 6: This is the shortest possible path with at most 1 obstacle elimination.
Example 2
  • Input: grid = [ [0, 1, 1], [1, 1, 1], [1, 0, 0] ] k = 1
  • Output: -1
  • Explanation: Step 1: The grid has 3 rows and 3 columns with obstacles at positions (0,1), (0,2), (1,0), (1,1), (1,2), and (2,0). Step 2: We can eliminate at most 1 obstacle. Step 3: To reach the bottom-right corner (2,2) from the top-left corner (0,0), we need to eliminate at least 2 obstacles. Step 4: Since k = 1, it's impossible to reach the destination. Step 5: Therefore, the output is -1.
Constraints
  • 1 <= grid.length, grid[0].length <= 40
  • grid[i][j] is either 0 or 1
  • 0 <= k <= grid.length * grid[0].length
  • grid[0][0] and grid[grid.length-1][grid[0].length-1] are guaranteed to be 0
  • Time Complexity: O(m * n * k), where m is the number of rows, n is the number of columns, and k is the maximum number of obstacles to eliminate
  • Space Complexity: O(m * n * k)
ArraysGoldman SachsDropbox
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use Breadth-First Search (BFS) to find the shortest path
  • Extend the visited state to include the number of obstacles eliminated so far
  • Define each state as (row, column, obstacles_eliminated)
  • Use a queue to process states in BFS order
  • For each state, explore all four possible directions: up, down, left, and right
  • If a new cell contains an obstacle, check if we can eliminate it (obstacles_eliminated < k)
  • Use a 3D visited array to avoid revisiting states with the same position and same number of eliminations

Steps to solve by this approach:

 Step 1: Use BFS to find the shortest path, as it guarantees the first path to reach the destination is the shortest.

 Step 2: Define a state as (row, column, obstacles_eliminated, steps) to track our progress.
 Step 3: Use a 3D visited array to avoid revisiting states with the same position and number of eliminations.
 Step 4: Start BFS from the top-left corner (0,0) and explore in all four directions (up, right, down, left).
 Step 5: For each new position, check if it's valid and if we've visited it with the same number of obstacles eliminated.
 Step 6: If the new position contains an obstacle, check if we can eliminate it (obstacles_eliminated < k).
 Step 7: When we reach the bottom-right corner, return the number of steps taken.

Submitted Code :