Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional warm-up

Problem: A runner moves up, down, left or right one cell at a time on this 5×5 grid. '.' is open, '#' is a wall. The runner starts at (row 0, col 0) and must reach (row 4, col 4), and may pass through at most ONE wall cell (entering it costs one move like any other step). ..#.. .##.# ...#. #...# ...#. What is the minimum number of moves?

  1. A. 8
  2. B. 9
  3. C. No route exists
  4. D. 10

Correct answer: A. 8

BFS over states (cell, walls used) finds an 8-move route that breaks one wall, e.g. down the left side and through (4,3), matching the Manhattan distance. Without the break the goal is sealed off, and a BFS whose visited set ignores the walls-used state also wrongly reports no route; counting cells instead of moves gives 9, and also charging an extra move for the break gives 10.

More Coding Interview Problems questions

Exams that ask this topic