Maze walk
Level B. Teaches a grid as a graph: cells as vertices, adj4 and bfs.
A maze has n rows and m columns (1 ≤ n, m ≤ 1000) of . (open) and # (wall). The top-left and bottom-right cells are open. In one step you move to an open cell that shares a side with yours. Print the fewest steps from the top-left to the bottom-right cell, or -1.
Input: n and m, then n lines of m characters. Output: one number.
| Input | Output |
|---|---|
3 4..#..#...... | 5 |
6 tests · Ctrl+Enter
Hints
- A cell
(r, c)overdomain Randdomain Colis a vertex;bfstakes a function from a cell to its neighbours instead of a built graph. c.adj4yields only the neighbours inside the grid, so there is no0 <= r < ncheck to get wrong.- Reading
[R, Col]Charreads the n lines of m characters in one go. - Graphs covers grids as graphs; the grid reference lists
adj4and the other neighbour walks.