Problem · Hard · 40 min
Shortest way out
- Hard
- Grids
- Arrays
A maze is an array of strings. All strings have the same length. Each character is one cell:
Sis the start. The maze has exactly oneS.Eis the exit. The maze has exactly oneE..is an open cell.#is a wall.
Write shortestPath(maze). It returns the smallest number of steps from S to E.
- One step moves up, down, left or right to a neighbouring cell that is inside the maze and is not a wall. You cannot move diagonally.
- If you cannot reach the exit, return
-1.
Examples
| Call | Result |
|---|---|
shortestPath(['S.E']) |
2 |
shortestPath(['S#E']) |
-1 |
shortestPath(['S.#', '..#', '#.E']) |
4 |
Tasks
2 to do
Task 1, not checked yet. Make
shortestPathpass the example tests.Task 2, not checked yet. Make
shortestPathpass the hidden tests.
Task 1 of 2
Make shortestPath pass the example tests.