Loading
Loading
A rescue drone must cross a collapsed building mapped as a grid. Cells marked 0 are open and cells marked 1 are rubble walls. The drone moves up, down, left or right one cell per step. It carries a limited number of demolition charges: entering a wall cell uses one charge and clears the way.
Starting in the top-left cell with charges charges, return the fewest steps needed to reach the bottom-right cell, or -1 if it cannot be reached. The start and goal cells are always open.
grid = [[0,0,0],[1,1,0],[0,0,0],[0,1,1],[0,0,0]], charges = 1without chargesthe zig-zag route takes 10 steps(0,0) → (1,0)blast through the wall below: 0 charges left(2,0) (3,0) (4,0)straight down the open left column(4,1) (4,2)along the bottom to the goal — 6 steps6Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 0 | 0 | 0 |
|---|---|---|---|
| 1 | 1 | 1 | 0 |
| 2 | 0 | 0 | 0 |
| 3 | 0 | 1 | 1 |
| 4 | 0 | 0 | 0 |
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(rows × cols × charges) time · O(rows × cols × charges) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 0 | 0 | 0 |
|---|---|---|---|
| 1 | 1 | 1 | 0 |
| 2 | 0 | 0 | 0 |
| 3 | 0 | 1 | 1 |
| 4 | 0 | 0 | 0 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Example 2
| 0 | 0 | 1 | 1 |
|---|---|---|---|
| 1 | 1 | 1 | 1 |
| 2 | 1 | 0 | 0 |
Every route needs to break through at least two walls.
Example 3
| 0 | 0 | 1 | 0 |
|---|---|---|---|
| 1 | 0 | 1 | 0 |
| 2 | 0 | 1 | 0 |