Loading
Loading
A warehouse robot moves across a floor plan of square cells. Cells marked 0 are clear and cells marked 1 hold shelving. The robot steps up, down, left or right, never diagonally, and only onto clear cells.
The robot starts in the top-left cell and must reach the bottom-right cell. Return the number of cells on the shortest route, counting both the start and the end, or -1 if no route exists (including when either end is blocked).
floor = [[0,0,0],[1,1,0],[0,0,0]]distance 1(0,0)distance 2(0,1)distance 3(0,2)distance 4(1,2)distance 5(2,2) — the goal5Sign 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 |
Example 2
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) time · O(rows × cols) 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 |
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.
| 0 | 0 | 1 | 0 | 0 |
|---|---|---|---|---|
| 1 | 0 | 1 | 0 | 1 |
| 2 | 0 | 0 | 0 | 0 |
| 3 | 1 | 1 | 1 | 0 |
Example 3
| 0 | 0 | 1 |
|---|---|---|
| 1 | 1 | 0 |
Moves are only up, down, left and right, so the two open cells never touch.