Practice

Problem 12 of 12

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:

  • S is the start. The maze has exactly one S.
  • E is the exit. The maze has exactly one E.
  • . 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

  1. Task 1, not checked yet. Make shortestPath pass the example tests.

  2. Task 2, not checked yet. Make shortestPath pass the hidden tests.

Task 1 of 2

Make shortestPath pass the example tests.

All tasks

main.js

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.