Company: OYO

Difficulty: medium

Problem Statement

Nearest Exit in a City Grid Given an `N` by `M` grid, find the fewest moves from the start cell to any exit. A cell is `0` when open, `1` or `2` when blocked, and `3` when an exit. You may move up, down, left, or right by one cell, and may enter open cells and exits. Input Format The first line contains `N M`. The next `N` lines each contain a string of `M` digits. The final line contains the zero-indexed start coordinates `R C`. Output Format Print the minimum number of moves to an exit, or `-1` if no exit is reachable. Constraints `1 <= N, M <= 500`, and the grid contains at least one `3`. Notes The start is guaranteed to be an open cell. If the start is an exit, the answer is zero.

More OYO OA questionsInterview experiences