Find the fewest steps to the nearest restaurant in a grid with walls

Quick Overview

Given a grid of open cells, walls, restaurants and one starting cell, find the fewest up, down, left or right steps to the closest reachable restaurant, or return -1 when none can be reached. It tests breadth-first search on a grid, visited-cell handling and edge cases such as a boxed-in start.

Find the fewest steps to the nearest restaurant in a grid with walls

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a 2D `grid` of characters where each cell contains one of the following: - `'0'` is an open path. - `'X'` is a wall and cannot be entered. - `'R'` is a restaurant. - `'S'` is the starting position. You can move up, down, left or right, one cell per step. You cannot move through walls or step outside the grid. Return the minimum number of steps needed to reach the nearest restaurant from the starting position. If no restaurant is reachable, return `-1`. ### Function Signature ```python def find_nearest_restaurant(grid: list[list[str]]) -> int: ``` ### Rules - The grid is a list of rows, each row is a list of one-character strings, and every row has the same length. - Any cell other than `'X'` can be entered. - The examples name cells as (row, column), counted from 0. ### Constraints - `1 <= len(grid), len(grid[0]) <= 100` - Each cell is one of `'0'`, `'X'`, `'R'` or `'S'`. - There is exactly one `'S'` in the grid. ### Examples ```text Input: grid = [ ["S", "0", "0", "X", "R"], ["X", "X", "0", "X", "0"], ["0", "0", "0", "0", "0"], ["0", "X", "X", "X", "R"], ["0", "0", "0", "0", "0"] ] Output: 7 ``` The path (0, 1), (0, 2), (1, 2), (2, 2), (2, 3), (2, 4), (3, 4) reaches the restaurant at (3, 4) in 7 steps, which equals its Manhattan distance from the start, so no shorter path exists. The restaurant at (0, 4) can only be entered from (1, 4), which is reached through (2, 4), so it takes 8 steps. ```text Input: grid = [ ["S", "X"], ["X", "R"] ] Output: -1 ``` Both cells next to the start are walls, so the start cannot move and the restaurant is unreachable. ```text Input: grid = [ ["S", "0"], ["0", "0"] ] Output: -1 ``` The grid has no restaurant at all, so the answer is -1.

Overview: Given a grid of open cells, walls, restaurants and one starting cell, find the fewest up, down, left or right steps to the closest reachable restaurant, or return -1 when none can be reached. It tests breadth-first search on a grid, visited-cell handling and edge cases such as a boxed-in start.

Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a 2D grid of characters where each cell contains one of the following:

  • '0' is an open path.
  • 'X' is a wall and cannot be entered.
  • 'R' is a restaurant.
  • 'S' is the starting position.

You can move up, down, left or right, one cell per step. You cannot move through walls or step outside the grid.

Return the minimum number of steps needed to reach the nearest restaurant from the starting position. If no restaurant is reachable, return -1.

Function Signature

def find_nearest_restaurant(grid: list[list[str]]) -> int:

Rules

  • The grid is a list of rows, each row is a list of one-character strings, and every row has the same length.
  • Any cell other than 'X' can be entered.
  • The examples name cells as (row, column), counted from 0.

Constraints

  • 1 <= len(grid), len(grid[0]) <= 100
  • Each cell is one of '0' , 'X' , 'R' or 'S' .
  • There is exactly one 'S' in the grid.

Examples

Input: grid = [
  ["S", "0", "0", "X", "R"],
  ["X", "X", "0", "X", "0"],
  ["0", "0", "0", "0", "0"],
  ["0", "X", "X", "X", "R"],
  ["0", "0", "0", "0", "0"]
]
Output: 7

The path (0, 1), (0, 2), (1, 2), (2, 2), (2, 3), (2, 4), (3, 4) reaches the restaurant at (3, 4) in 7 steps, which equals its Manhattan distance from the start, so no shorter path exists. The restaurant at (0, 4) can only be entered from (1, 4), which is reached through (2, 4), so it takes 8 steps.

Input: grid = [
  ["S", "X"],
  ["X", "R"]
]
Output: -1

Both cells next to the start are walls, so the start cannot move and the restaurant is unreachable.

Input: grid = [
  ["S", "0"],
  ["0", "0"]
]
Output: -1

The grid has no restaurant at all, so the answer is -1.