Maze Runner
MetaOnsiteMedium
You are given a maze as a list of equal-length strings, one per row. S marks the start and E the exit, and each appears exactly once. # is a wall that cannot be entered and . is open floor. Each move goes one cell up, down, left or right and costs 1 step.
Implement shortest_path(grid), which returns the fewest steps needed to walk from S to E, or -1 if E cannot be reached. The parts below add rules to the maze one at a time.
Part 1: Shortest path
Find the fewest steps from S to E, moving between orthogonally adjacent open cells. In this part the grid contains only S, E, # and .. Return -1 if E cannot be reached.
- Example 1
- grid
["S..#","#.#.","...E"]- Output
5
Why: The only route goes right once, down twice, then right twice into
E: 5 steps; the walls leave nothing shorter.
Constraints
1 <= len(grid) <= 501 <= len(grid[i]) <= 50grid[i][j]is one ofS,E,#,.