Corn Maze Shortcut
A farm cuts a maze into its corn field every autumn. The map is rows of characters: # is corn you cannot walk through, . is a path, S is the entrance and E the exit. Visitors move one square at a time, up, down, left or right.
The owner prints the fastest route's length on the ticket for visitors to beat. Some years a careless cut seals off the exit, and the ticket should say so.
Your task
Write shortestWalk(maze). maze is a list of equal-length strings. Return the smallest number of steps to get from S to E, where each step moves to a neighboring square up, down, left or right that is not #.
Return -1 if there is no way to reach E. You cannot step outside the map.
shortestWalk(maze: string[]) → int
Examples
maze = ["S.#.", "..#E", "#...", "...."]
6
The wall in column 2 blocks the top two rows. One fastest route: down to (1,0), right to (1,1), down to (2,1), right to (2,2) and (2,3), up to E at (1,3). 6 steps; nothing shorter gets around the wall.
maze = ["S#E"]
-1
The only square between S and E is corn and there is nowhere else to go, so the answer is -1.
Constraints
- 1 ≤ rows, cols ≤ 20
- Each character is '#', '.', 'S' or 'E'
- There is exactly one 'S' and exactly one 'E'
Start with brute force
Try every route with a depth-first search: walk from S, try each direction, back up at dead ends, and keep the shortest route to E. (The first route depth-first search finds is often a long winding one.)
On an open 20 × 20 field that is hopeless. There are only 400 squares, but there is an astronomical number of simple paths, and most of them walk through squares that could have been reached faster another way.
Hints
0 of 4 shown- Hint 1
Take a pencil and the first example. Write 0 on S, 1 on every square one step away, then 2 on every new square next to a 1. What do you notice when you reach E?
- Hint 2
The numbers spread in rings, like ripples. A square gets its smallest step count the first time a ring touches it and never needs another.
- Hint 3
Which data structure lets you finish all the squares at distance 1 before starting any at distance 2? A stack does not do that, but another structure does.
- Hint 4
Run a breadth-first search from S with a queue. Store each square's step count when you first add it, and mark it visited then. Skip walls, squares off the map and visited squares. The first time you take E out of (or put E into) the queue, its count is the answer. If the queue empties first, return -1.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
When every step costs the same, breadth-first search finds shortest routes for free. It handles all squares at distance 1, then 2, and so on, so it first reaches each square along a shortest route.
Trace the first example. The map is S.#. / ..#E / #... / ...., with S at (0,0) and E at (1,3).
- At distance 0 there is only S at (0,0).
- At distance 1 are (1,0) and (0,1). Their other neighbors are walls, the map edge, or each other.
- At distance 2 is (1,1). The square below (1,0) is
#. - At distance 3 is (2,1).
- At distance 4 are (2,2) and (3,1).
- At distance 5 are (2,3), (3,2) and (3,0).
- At distance 6 is (1,3), which is E, reached from (2,3), so the function returns 6.
Mark a square visited when you add it to the queue, not when you take it out. Otherwise several neighbors can add the same square, wasting work and, on open fields, blowing up the queue.
function shortestWalk(maze) {
const rows = maze.length, cols = maze[0].length;
let start = null;
for (let r = 0; r < rows; r++) {
const c = maze[r].indexOf('S');
if (c !== -1) start = [r, c];
}
const dist = maze.map(row => new Array(cols).fill(-1));
dist[start[0]][start[1]] = 0;
const queue = [start];
for (let head = 0; head < queue.length; head++) {
const [r, c] = queue[head];
if (maze[r][c] === 'E') return dist[r][c];
for (const [nr, nc] of [[r + 1, c], [r - 1, c], [r, c + 1], [r, c - 1]]) {
if (nr < 0 || nr >= rows || nc < 0 || nc >= cols) continue;
if (maze[nr][nc] === '#' || dist[nr][nc] !== -1) continue;
dist[nr][nc] = dist[r][c] + 1; // first visit is the shortest
queue.push([nr, nc]);
}
}
return -1; // queue ran out before reaching E
}from collections import deque
def shortest_walk(maze):
rows, cols = len(maze), len(maze[0])
start = None
for r in range(rows):
c = maze[r].find('S')
if c != -1:
start = (r, c)
dist = [[-1] * cols for _ in range(rows)]
dist[start[0]][start[1]] = 0
queue = deque([start])
while queue:
r, c = queue.popleft()
if maze[r][c] == 'E':
return dist[r][c]
for nr, nc in ((r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)):
if 0 <= nr < rows and 0 <= nc < cols and maze[nr][nc] != '#' and dist[nr][nc] == -1:
dist[nr][nc] = dist[r][c] + 1 # first visit is the shortest
queue.append((nr, nc))
return -1 # queue ran out before reaching ERun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.