bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1293. Shortest Path in a Grid with Obstacles Elimination

1293. Shortest Path in a Grid with Obstacles Elimination

Leetcode Solutions

BFS (Breadth-First Search) with Obstacle Elimination

  1. Initialize a queue and add the starting position (0, 0) with 0 steps taken and 0 obstacles eliminated.
  2. Initialize a set to keep track of visited states (position and number of obstacles eliminated).
  3. While the queue is not empty, pop the front element, which contains the current position, steps taken, and obstacles eliminated.
  4. If the current position is the target (bottom-right corner), return the number of steps taken.
  5. Explore all four possible directions from the current position (up, down, left, right).
  6. For each direction, if moving to the next position is within the grid boundaries and the number of obstacles eliminated is within the limit k, add the new state to the queue and mark it as visited.
  7. If the next position is an obstacle and we have not reached the limit of obstacles that can be eliminated, increase the count of obstacles eliminated and add the new state to the queue.
  8. If the target is not reachable, return -1.
UML Thumbnail

A* (A Star) Search Algorithm

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...