You're navigating through a dangerous maze represented by an m ร n grid where each cell is either 0 (safe passage) or 1 (deadly obstacle). Your mission: find the shortest path from the top-left corner (0, 0) to the bottom-right corner (m-1, n-1).
Here's the twist: you have a special power that allows you to eliminate up to k obstacles along your journey! You can move in four directions (up, down, left, right), but only through empty cells or obstacles you choose to eliminate.
Goal: Return the minimum number of steps needed to reach the destination. If it's impossible even with your obstacle-elimination power, return -1.
Example: In a 3ร3 grid with obstacles, you might need to eliminate 1 obstacle to create the shortest path of 6 steps instead of taking a longer detour.
Input & Output
Visualization
Time & Space Complexity
We might visit each cell with each possible elimination count, and BFS visits each state at most once
Queue and visited set can store up to m*n*k states
Constraints
- m == grid.length
- n == grid[i].length
- 1 โค m, n โค 40
- 1 โค k โค m ร n
- grid[i][j] is either 0 or 1
- grid[0][0] == grid[m-1][n-1] == 0